Sql Server Database connection using Configuration manager in Visual Studio Asp.Net
Introduction : This tutorial explain how to connect sql server database using configuration manager by using visual studio. and what the attribute add in web.config file.
In Web.config file you need to following connection properties.
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="constr" connectionString="Data Source=HOME-PC; Initial Catalog=YourDatabaseName;Integrated Security=True;" providerName="system.data.sqlclient" />
</connectionStrings>
</configuration>
Here Meaning of above property is like belowe :
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="constr" connectionString="Data Source=HOME-PC; Initial Catalog=YourDatabaseName;Integrated Security=True;" providerName="system.data.sqlclient" />
</connectionStrings>
</configuration>
Here Meaning of above property is like belowe :
1> add name="constr" : here constr is connection string name this string used for configuration manager to set connection
2> connectionString : this field define three database property as below
<1 : Data Source=HOME-PC; Here HOME-PC is server name Of Sql Server database it can be set when you install sql server setup
<2 : Initial Catalog=YourDatabaseName : Here in this field give your database name
<3 : Integrated Security=True : here this fiel provides the database security
3> : providerName="system.data.sqlclient : This field provide the name space for sql server to access these class library.
Now in Visual Studio you can add following name space and set connection string as following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebSearch
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
}
}
here, con is new Sqlconnection connection Object using these you can open and close the connect of sql server database.
Now in Visual Studio you can add following name space and set connection string as following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebSearch
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
}
}
here, con is new Sqlconnection connection Object using these you can open and close the connect of sql server database.
See Other Tutorial :
Sign up here with your email
ConversionConversion EmoticonEmoticon