Method one: Web. config file
--Settings:
Adding keywords to the Web. config file key is implemented by the <appSettings> tag, but the appSettings tag is usually placed in the <system.web>.....</system.web > Mark out. Cases:
<configration>
<appSettings>
<add key= "connString1" value= "Server=localhost;user id=sa;pwd=;d atabase= database name"/>
<add key= "connString2" value= "Provider=Microsoft.Jet.OLEDB.4.0;Data source= database Path"/>
</appSettings>
<system.web>
</system.web>
</configration>
--read:
To reference these database connection strings in your code, You need to first add a reference to the System.Configuration namespace that contains the ConfigurationSettings class, whose static method ConfigurationSettings.AppSettings property gets the Web. config text Set in the <appSettings> section of the piece, the value read is a string type. For example:
Using System.Configuration;
String conn1 = configurationsettings.appsettings["ConnString1"];
String conn2 = configurationsettings.appsettings["ConnString2"];
SQLConnection myConn1 = new SQLConnection (CONN1);
OleDbConnection myConn2 = new OleDbConnection (CONN2);
In VS2005, configurationsettings.appsettings can be replaced by configurationmanager.appsettings.
Method Two: Gloab file
--Settings:
In the global file, add
protected void Session_Start (Object sender, EventArgs e)
{
session["sqlconnectionstring"] = "uid=username;pwd=password;database=mytest;server=localhost; Connect timeout=300 ";
}
--read:
Application in code:
String strconnection=session["sqlConnectionString"]. ToString ();
Sqlconnection_1=new sqlConnection (strconnection);