473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to use web.config for sql connection string

Hi,
I heard you could put your db connection string into the
web.config file to make it editable after you compile
and/or deploy.
I cant seem to find any info on this.. can any one help?
Sample code would be nice
Cheers
Steve

Nov 17 '05 #1
3 7751
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="LDAPConnectString" value="LDAP://Domain.com/DC=Domain,DC=com" />
</appSettings>
</configuration>
in code.
using System.Configuration

string sLDAP =
ConfigurationSettings.AppSettings["LDAPConnectString"].ToString();

"steve" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Hi,
I heard you could put your db connection string into the
web.config file to make it editable after you compile
and/or deploy.
I cant seem to find any info on this.. can any one help?
Sample code would be nice
Cheers
Steve

Nov 17 '05 #2
<configuration>
<appSettings>
<add key="DSN" value="Server=(local);Database=DBName;UID=sa;PWD="/>
<add key="OtherConnectionString"
value="Server=(local);Database=DBName2;UID=sa;PWD= "/>
</appSettings>
</configuration>

This is a nice way to manage it. You can change the connection string
easily without rebuilding the app or restarting IIS or anything, and the
change goes into effect immediately.

Then in your code behinds you can get the connection string like this:
Dim sConn As String = ConfigurationSettings.AppSettings("DSN")

You'll want to put that line on every page that does data access. Open the
connection just before you need it and close it as soon as you are done with
it. Automatic connection pooling in ADO.NET makes this a very efficient
technique.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"steve" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Hi,
I heard you could put your db connection string into the
web.config file to make it editable after you compile
and/or deploy.
I cant seem to find any info on this.. can any one help?
Sample code would be nice
Cheers
Steve

Nov 17 '05 #3
I put our connection strings in our machine.config file in
c:\(%windir%)\Microsoft.Net\Framework\(%Version%)\ Config
Like this:
<appSettings>
<add key="connMainDB1" value="data source=ipaddy;initial
catalog=db1;persist security info=False;user
id=blah;password=blah;workstation id=webserver;packet size=4096"/>
<add key="connMainDB2" value="data source=ipaddy;initial
catalog=db2;persist security info=False;user
id=blah;password=blah;workstation id=webserver;packet size=4096"/>
</appSettings>

then, in your app you can read that out by:
Dim strConn As String =
System.Configuration.ConfigurationSettings.AppSett ings("connMainDB1")

Of course, you can do the same thing in web.config.
Good Luck!

Bill

"steve" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Hi,
I heard you could put your db connection string into the
web.config file to make it editable after you compile
and/or deploy.
I cant seem to find any info on this.. can any one help?
Sample code would be nice
Cheers
Steve

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Robert | last post by:
I have what I think should be a pretty common scenario in a corporate environment. I have a web app and three environments on three servers: Development, QA and Production. We develop in...
3
by: Tim Gallivan | last post by:
Hi all, I think read somewhere (but I can't find it ... note to self: must get new filing system ...) that there is a workaround so that an app.config can have multiple keys with the same name...
14
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review,...
5
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from...
8
by: theWizard1 | last post by:
Using Asp.NET 1.1, and C#. I have a directory for the website, and a directory under it named Secure. I have a web.config in each of the above directories. The web.config in the Secure...
2
by: malice | last post by:
I have been searching the internet for 12+ hours on how to do this. At this point I'm about to give up. I am using VB .Net 2.0 with the enterprise library jan 2006 release. This is my first time...
12
by: Ben | last post by:
I have a group of settings that I'd like to have inherited by multiple sites. I'm trying this, but it's not working. wwwroot\group\web.config wwwroot\group\site1\web.config...
10
by: eagle | last post by:
I have a web.config in my application that contains the connection strings to all my datasources. I want to move these connection strings to another web config up the folder hierarchy so that all...
2
by: bz | last post by:
Hi, I have a library project that implements a Business Layer for a web and a desktop application All my business classes are in this lib, so I have here the connection string to database as...
2
by: Johnson | last post by:
I'm trying to fix a "sub optimal" situation with respect to connection string management. Your thoughtful responses will be appreciated. I just started with a new client who has a bunch of legacy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.