ASP. NET 2.0 traverses the database connection string of the configuration file

Source: Internet
Author: User

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 ");

In addition, if output is performed during traversal, an additional configuration item is displayed because a database connection is defined by default in machine. config. The content is as follows:

<Connectionstrings>
<Add name = "localsqlserver" connectionstring = "Data Source =. sqlexpress; Integrated Security = sspi; attachdbfilename = | datadirectory | aspnetdb. MDF; user instance = true "providername =" system. data. sqlclient "/>
</ConnectionStrings>

This is why many netizens often ask on the Forum: Why does my program call the SQLEXPRESS database? If your database configuration is incorrect or cannot be enabled, the SQLEXPRESS database is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.