Wednesday 13 June 2012

What is the best place to store Database connection string in .Net?


In Web.Config, you would add a key to the AppSettings Section:
<appSettings>

<add key="MyDBConnection" value="data source=<ServerName>;Initial catalog =<DBName>;user id=<Username>;password=<Password>;" />

</appSettings>
Example:

<add key="ConnectionString" value= "data source=localhost;Initial catalog=northwind;user id=sa;password=mypass" />

Then, in your ASP.Net application - just refer to it like this:

using System.Configuration;

string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];

No comments:

Post a Comment