SQL Connection in app.config question...

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.
 
G

Guest

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
 
G

Guest

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.
 

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

Top