Read ConnnectionString from web.config in C#
ConnectionStringis normally stored in web.config and it is recommended to store connectionstring in web.config file. To use connectionstring in our application we need to read connectionstring from web.config.
We need to use ConfigurationManager class in order to read connectionstring from web.config. You may need to add reference for assembly “System.Configuration” before using ConfigurationManager class.
string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);
Web.Config
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="YourConnectionStringHere" />
</connectionStrings>
Comments
Post a Comment