Asp.net| Traversal | string
In ASP.net 2.0, a more convenient configuration file access class is provided, which can be viewed under the System.Configuration namespace. This article provides a common way to get a database string in the development process, and write a method to make it easy to use:
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 the web.config is configured as follows:
If written as a static class method, you can invoke it using the following method:
String connectstring = xianhuimengutil.getconnectionstring ("ConnectionString1");
In addition, if you are traversing the output, you can see more than one configuration item, that is because the machine.config has the default definition of a database connection, the contents are as follows:
This is what many netizens often ask in forums: why my program calls the SQLEXPRESS database, if your database is not configured correctly, or if you cannot open it, you will use the SQLEXPRESS database.