How to store connection string in congif file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..
 
Hi Perin,
In web.config file, add the section below.

<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;password=secret;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()

Very simple..

Rgds,
John Paul. A
 
Just remember to add the following import statement at the top of any
module/Codebehind/class that will be using

ConfigurationSettings.AppSettings("ConnectionString")


Imports System.Configuration.ConfigurationSettings
 
In web.Config you have an <appSettings> section.
Insert following lines
<add key="CS"
value=" Write connection string here." />

From codebehind pages access CS by
Me.SQLConnection1.ConnectionString =
System.Configuration.ConfigurationSettings.AppSettings("CS")

HTH.
Regards
Jignesh.
 
Hello John,

Even better would be a class that wraps the ConfigurationSettings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionString { get { return ConfigurationSettings.AppSettings["ConnectionString"];
} }
}

then: SqlConnection conn = new SqlConnection(MyConfiguration.ConnectionString);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.
 
Dear MAtt Berther,
What you are saying is right.
Thanks for your guidance.

Rgds,
John Paul. A

Matt Berther said:
Hello John,

Even better would be a class that wraps the ConfigurationSettings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionString { get { return ConfigurationSettings.AppSettings["ConnectionString"];
} }
}

then: SqlConnection conn = new SqlConnection(MyConfiguration.ConnectionString);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.

--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;password=secret;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..

strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..

Rgds,
John Paul. A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top