This document describes how to use global variables in two ASP. NET projects. The Web. config file and the gloab file. Method 1: In the Web. config file -- settings: Add a keyword key to the Web. config file using the <etettings> tag?
......
-----------------------------------------------------------------------------
This document describes how to use global variables in two ASP. NET projects. The Web. config file and the gloab file. Method 1: Web. config file
-- Settings:
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>
</System. Web>
</Configration>
-- Read:
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 );
In vs2005, configurationsettings. configurettings can be changed to configurationmanager. configurettings.
Method 2: gloab File
-- Settings:
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 );
The first method is recommended! Flexible