If we write the database connection information, that is, the database connection string, in web. config, it not only enhances program security, but also greatly facilitates program modification.
In the web. the keyword key is added to the config file through the <ettings> mark, but the ettings mark is usually placed in the <system. web> ..... </system. web> mark the outside. Example:
<Configration>
<Deleetask>
<Add key = "connString1"
Value = "server = localhost; user id = sa; pwd =; database = database name"/>
<Add key = "connString2"
Value = "provider = Microsoft. Jet. OleDb.4.0; Data Source = database path"/>
</AppSettings>
<System. web>
To reference these database connection strings in the code, you must first add. conFiguration namespace reference, which contains the ConfigurationSettings class, its static method ConfigurationSettings. the deleetaskproperty can be used to obtain the web. in the config file, set the value to string. For example:
Using System. Configuration;
String conn1 = ConfigurationSettings. receivettings ["connString1"];
String conn2 = ConfigurationSettings. receivettings ["connString2"];
SQLConnection myConn1 = new SQLConnection (conn1 );
OleDbConnection myConn2 = new OleDbConnection (conn2 );
.
.
.
</System. web>
</Configration>