How to store connection string in congif file

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..
 
G

Guest

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
 
L

Leo

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
 
J

Jignesh Desai

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.
 
M

Matt Berther

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.
 
G

Guest

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top