Translate

Saturday, February 18, 2012

How to read connection string from web.config (asp.net)

To read connection string from web.config do the following

1>Store connection string in web.config

For using Windows Authentication, your connection string would look like

<configuration>
  <connectionStrings>
    <add name="myConnectionString" connectionString="Data Source=MyServerName;Initial Catalog=MyDataBaseName;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

For using SQL Authentication, your connection string would look like

<configuration>
  <connectionStrings>
<add name="myConnectionString" connectionString="Data Source=MyServerName;Initial Catalog=MyDataBaseName;User ID=mySqlUserName;Password=myPassWord"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>



2>Read connection string from C# code

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;



No comments:

Post a Comment

Comments will appear once they have been approved by the moderator