You can read connection string from web.config file in ASP.NET by using the ConfigurationManager class in case you want to read it in to a property in your .NET class library.
How to Read connection string from web.config in ASP.NET?
- Ensure that you add System.Configuration as a reference in your Visual Studio Solution.
- In you C# code, where you want to read the connection string from the config , use the below code snippet.
string conn = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
The variable conn should now have the connection string that is read from your web.config.
Enter your email address to subscribe to this blog and receive notifications of new posts by email.