In. NET Framework 2.0, the configurationmanager class adds two attributes: deleettings and connectionstrings, which are used to obtain data in the configuration file appsettings and connectionstrings sections. The usage is as follows:
Using system. configuration;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Label1.text = configurationmanager. connectionstrings ["connectionstringsname"]. tostring ();
Label2.text = configurationmanager. appsettings ["appsettingsname"]. tostring ();
}
}
The difference between <connectionstrings> and <etettings>
1. <connectionstrings>
<Connectionstrings>
<Add name = "syxtconnectionstring" connectionstring = "Data Source = leike; initial catalog = syxt; user id = sa; Password = sa"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>
2. <appsettings>:
<Add key = "connectionstring1" value = "Data Source = 192.168.123.201; initial catalog = webmedlink; persist Security info = false; user id = sa; Password = ksdchagd2004; packet size = 4096 ">
</Add>
Note:
(1) appsonstrins are commonly used in 2003 and connectionstrins are commonly used in 2005.
(2) advantages of using connectionstring:
First, you can encrypt the connection string by using an encryption tool of Ms.
Second, data source controls that can be directly bound without having to writeCodeRead it and assign it to the control.
Third, you can easily change the database platform. For example, if you change to an Oracle database, you only need to modify the providername.
(3) Use System. configuration. configurationmanager. deleetask[ "name"] to retrieve the value in <deleetask>.
In <connectionstrings>, use system. configuration. configurationmanager. connectionstrings ["name"] to retrieve the value.