SQL ConnectionString in web.config

  • Thread starter Thread starter Islam Elkhayat
  • Start date Start date
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??
 
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
 
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
 
Back
Top