Store and Reading connection string in web.config
Handy Tips for freshers:
Connection string in .NET 3.5 (and above) config file:
Do not use appsettings in web.config. Instead use the connectionStrings section in web.config.
<add name="myConnectionString" connectionString="server=\SQLEXPRESS;database=myDb;uid=myUser;password=myPass;" />
To read/access the connection string into your code, use the ConfigurationManager class.
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Always store the connection string in a config file.
I saw many freshers save connection string on page(Hard Coding) or in session, Never be among them.
Connection string in .NET 2.0 config file
In the appSettings location, add a key named whatever you like to reference your connection string to.
To read the connection string from code, use the ConfigurationSettings class.
string connStr = ConfigurationSettings.AppSettings("myConnectionString");
Hope this will help few!
Ref: https://www.connectionstrings.com/store-connection-string-in-webconfig/
Recent Comments