C #
Copy code
System. configuration. Configuration rootwebconfig = system. Web. configuration. webconfigurationmanager. openwebconfiguration ( "/Mywebsiteroot" ); System. configuration. connectionstringsettings connstring; If (0 <rootwebconfig. connectionstrings. connectionstrings. Count) {connstring = rootwebconfig. connectionstrings. connectionstrings [ "Northwindconnectionstring" ]; If ( Null ! = Connstring) console. writeline ( "Northwind connectionString = \" {0 }\ "" , Connstring. connectionstring ); Else Console. writeline ( "No northwind connectionString " );} CompileCode
This example requires:
In the root web. config fileConnectionstringsElement, which containsNorthwindconnectionstring. This element may be as follows:
Copy code
connectionstrings an element is a direct sublevel of an element, and system. equivalent of an element.