Search
Categories
  ASP.NET
Visual C#
Visual Basic
.NET Framework
ADO.NET
AJAX
LINQ
Java
 
Tutorial Rating
 
3.2 out of 5


Total Votes: 89
         
1 2 3 4 5
 




Storing Database Connection Strings in App.Config

Author: Waqas Anwar - Posted Date: 08-April-2008 - Category: Visual C#   
Bookmark and Share
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;




RELATED TUTORIALS

  Connection String Encryption in Web.config using VB.NET
  Connection String Encryption in Web.config using C#
  Using SqlConnectionStringBuilder class in C#
  Application Configuration Settings in .NET

LEAVE YOUR COMMENTS LEAVE YOUR COMMENTS
 Name (required)  
 Email (required
 Website


 
POSTED COMMENTS POSTED COMMENTS

thank you, lovely article
Posted by luckykabutar on Sunday, May 25, 2008

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
Posted by Vuyiswa on Monday, June 16, 2008

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
Posted by satya06.ag on Tuesday, November 18, 2008

Nice but need more clarification
Posted by Sankar on Saturday, January 24, 2009

What about for MS Access database can we use this process.
Posted by John on Wednesday, April 01, 2009

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
Posted by mukharanja on Friday, April 17, 2009

Hi mukharanja

You need to add System.Configuration Reference in your Project.

Right Click on Project Name in Solution Explorer and click Add Reference
Locate System.Configuration and add its reference your problem will solve.
Posted by Waqas Anwar on Saturday, April 18, 2009

nice
Posted by Adnan Ghaffar on Friday, August 28, 2009

please more elaborate it , specially.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
why did you use " ["MyDBConnectionString"] " .
thanks.
Posted by Gentleman on Tuesday, September 01, 2009

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.
Posted by Waqas Anwar on Tuesday, September 01, 2009

Thank you!
Posted by Mo on Thursday, March 11, 2010

How to configure database path setting in .inf file
for client users to access them............
Posted by Jagatpal Singh on Tuesday, July 20, 2010

 


 
 
 

Categories

My Portfolio

Website Links


Copyright @ 2009 EzzyLearning.com