Storing database connection strings in source code can lead you to some security issues and can also cause you maintenance problems. It is always recommended that you keep your connection strings in a separate configuration file so that you can change database connection related information such as password or database name without even modifying or recompiling your source code.
.NET Framework 2.0 added a separate configuration section in both Windows Application configuration (App.config) and Web Application configuration (Web.config) file. Developers can use these section to store connection string information such as a connection string name or provider type etc. In the following tutorial I will show you how you can store and retrieve connection string information in .NET Windows Application using C#.
The following code shows how you can store connection strings in App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
</configuration>
Once you have saved your connection string in App.config file you can use System.Configuration.ConfigurationManager class to read this connection string in code.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
ConnectionStringsSettings class provides properties to read connection string settings in your program as following code shows.
string name = conSettings.Name;
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;
Hi
How I modify my app.config using vb .net
In the above post you have explicitly explained reading connection string from the app file. Could you please help me how to, programmatically, write a connection string into app.config file. Please provide the code in visual basic.net.
Hello
Very nice tutorial.. Thanks! nicely explained
gud one thanks alot:)
It works
How to configure database path setting in .inf file
for client users to access them…………
Thank you!
please more elaborate it , specially.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[“MyDBConnectionString”];
why did you use ” [“MyDBConnectionString”] ” .
thanks.
ssssvvvvvs
Hi Gentleman
The name of the connection string I have defined in configuration file is MyDBConnectionString that’s why I am using MyDBConnectionString to read connection string from configuration file.
please more elaborate it , specially.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[“MyDBConnectionString”];
why did you use ” [“MyDBConnectionString”] ” .
thanks.
nice
I worked on your code, but class ConfigurationManager is not recoginzing .
throwing error ConfigurationManager not exists.
Is there any dll or assembly has to download and install to solve this error?
thanks
What about for MS Access database can we use this process.
Nice but need more clarification
The code for storing connection string in app.config file is very helpful for me. And also it is very simpl to understand.
Thank you for this solution.
I have one more request that please send me the settings for connection string in web.config file for asp.net and also the settings for login as i wanted to display my login page for all the request which directly coming from browser.
Waiting for reply.
Thank you
Am Using VB 2003, and the ConfigurationManager is not Recognised. and am not doing a Web Application , its a Windows Application. How can i retrieve the Connection string in my Win App
Thanks
thank you, lovely article