ASP. NET 2.0 provides a more convenient class for accessing configuration files. For details, see the system. Configuration namespace. This article provides a method that is commonly used in the development process to obtain database strings. For ease of use, write a method for calling:
Public String getconnectionstring (string _ connectionstringsname)
{
System. configuration. connectionstringsettingscollection Config = system. configuration. configurationmanager. connectionstrings;
For (INT I = 0; I <config. Count; I ++)
{
If (config [I]. Name. Equals (_ connectionstringsname, stringcomparison. ordinalignorecase ))
Return config [I]. tostring ();
}
Return string. empty;
}
If Web. config is configured as follows:
<Connectionstrings>
<Add name = "connectionstring1" connectionstring = "Persist Security info = false; user id = sa; Password =; initial catalog = database1; server = (local ); "providername =" system. data. sqlclient "/>
<Add name = "connectionstring2" connectionstring = "Persist Security info = false; user id = sa; Password =; initial catalog = database2; server = (local ); "providername =" system. data. sqlclient "/>
</Connectionstrings>
If you write static class methods, you can use the following method to call them:
String connectstring = xianhuimengutil. getconnectionstring ("connectionstring1 ");