Search
Tutorials
How TOs
Home
Top Tutorials
HOW TOs
Presentations
Web Services
Cheat Sheets
About Me
Contact Me
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
Saving...
Amazon.co.uk Widgets
Storing Database Connection Strings in App.Config
Author:
Waqas Anwar
-
Posted Date:
08-April-2008
-
Category:
Visual C#
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
Name (
required
)
Email (
required
)
Website
POSTED COMMENTS
thank you, lovely article
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
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
Nice but need more clarification
What about for MS Access database can we use this process.
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
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.
nice
please more elaborate it , specially.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
why did you use " ["MyDBConnectionString"] " .
thanks.
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.
Thank you!
How to configure database path setting in .inf file
for client users to access them............
Categories
ASP.NET
Visual C#
Visual Basic
.NET Framework
ADO.NET
AJAX
LINQ
Java
My Portfolio
My Portfolio Summary
Teaching and Training Experience
Speaking Accomplishments
Work Experience
Professional Skillset
Industry Honors and Awards
Educational Background
My Projects
Personal Information
Website Links
Programming HOW TOs
My Presentations
Free XML Web Services
Cheat Sheets
.NET Code Converter
.NET Framework Class Browser
About EzzyLearning
Advertise With Us
Subscribe to RSS Feeds
Copyright @ 2009 EzzyLearning.com