SQL Connection in app.config question...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to specify the connection string to the SQL Server so that I can just
refer to it instead of the full connection string.

Here's what I have:
<app.config>
<appSettings>
<add key="SQLDBConn",value="Server=SqlSrvr;Database=DBName;User
ID=uid;Password=pwd;Trusted_Connection=False" />
</appSettings>

Then in my code I replace the connection string with the key "SQLDBConn":
SqlConnection conn = new SqlConnection(SQLDBConn);

But I get an error when I compile that it does not exist in the namespace.

What have I done wrong?

Thanks.
 
Joe,

string sqlDbConn = ConfigurationManager.AppSettings["SQLDBConn"];

1) need a reference to System.Configuration assembly.
2) you can make this static and put it in Global.asax (at class level) and
refer to it from any page as "Global.sqlDbConn". You can also just store it
in Application State by reading and assigning in the Global Application_Start
handler method.
3) This should really have been in the ASP.NET group, but I guess that's a
lost cause.
Peter
 
The way you access the connection string from the web.config is different. I
see that Peter Bromberg has posted the solution, that should help you.
 
Back
Top