connection string in Web.Config

J

John A Grandy

If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
G

Guest

Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);
 
B

blackstaronline.net

Here is what works for me;

--->In your WEB.CONFIG file
<appSettings>
<add key="DBConn" value="Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\inetpub\wwwroot\data\test.mdb" />
</appSettings>

-->In your page
Dim objConn As OleDBConnection = New
OleDBConnection(ConfigurationSettings.AppSettings("DBConn"))

Hope this helps,
Jeremy Reid
http://blackstaronline.net/hgtit
 
G

Guest

Sorry, I am very much in the mindset of ASP.NET 2.0. they have added some
nice additional configuration management features. For 1.1 try:

web.config:
<appSettings>
<add key="ConnectionString" value="server=...;database=...;user id=...;
password=...;" />
</appSettings>

In your connection code:
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

--
brians
http://www.limbertech.com

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
J

John A Grandy

Yes. I am doing 2.0 , so this is what I want. Thanks.

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
R

rabram66

John said:
Yes. I am doing 2.0 , so this is what I want. Thanks.

brians said:
Hello John,

To get that string try:

// Get the connectionString.
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings["GUIDBConnectionString"];

Then to use it:
// Initialize the connection with the string
SqlConnection sqlConnection - new
SqlConnection(connectionStringSettings.ConnectionString);

--
I think that should do it - brians
http://www.limbertech.com


John A Grandy said:
If a SQLDataSource add the following to my web.config file, how to I
programmatically extract the connection string:

<configuration>

<appSettings/>

<connectionStrings>

<add name="GUIDBConnectionString" connectionString="Data
Source=(local);Initial Catalog=GUI;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>
 
R

rabram66

I tried to use:
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"])
in my connection code, but I got the error message below.

'Public Shared ReadOnly Property AppSettings() As
System.Collections.Specialized.NameValueCollection' is obsolete: 'This
method is obsolete, it has been replaced by
System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

Is there something I should be doing differently in asp.net 2.0?
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top