SQL ConnectionString in web.config

I

Islam Elkhayat

In my C# web application i have a dbacess.cs class contain my Sql
connection and all methods that's connect with the database...
Even my connection string in a seprate class i read in many sites that a
more secure to keep it in web.config file but i don't know how to implememnt
it in the web.config & how can i call it in my class or anywhere in my
application... Can anyone help me??
 
I

Islam Elkhayat

thanx for ur Quick reply
but this page is about authentication information
but i t'm asking about how to store sqlconnection string and call it from
the web.config
 
S

Steve C. Orr [MVP, MCSD]

OK, so it sounds like you want the simpler, slightly less secure method.
Then here's what the appSettings section of your web.config should look
like:
<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
 

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

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top