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:
The following are the referenced contents: 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:
The following are the referenced contents:
|
If written as a static class method, you can invoke it using the following method:
The following are the referenced contents: 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:
The following are the referenced contents:
|
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.