How to obtain database strings using ASP. NET

Source: Internet
Author: User

How to obtain database strings using ASP. NET

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:

Reference content is as follows:

 
 
  1. publicstringGetConnectionString(string_connectionStringsName)  
  2. {  
  3. SystemSystem.Configuration.ConnectionStringSettingsCollectionconfig=  
  4. System.Configuration.ConfigurationManager.ConnectionStrings;  
  5. for(inti=0;i<config.Count;i++)  
  6. {  
  7. if(config[i].Name.Equals(_connectionStringsName,StringComparison.OrdinalIgnoreCase))  
  8. returnconfig[i].ToString();  
  9. }  
  10. returnString.Empty;  


If web. config is configured as follows:

Reference content is as follows:

 
 
  1. <connectionStrings>  
  2. <add name="ConnectionString1" connectionString="Persist Security Info=False;  
  3.        User ID=sa;  
  4.        Password=;  
  5.        Initial Catalog=DataBase1;  
  6.        Server=(local);"  
  7.    providerName="System.Data.SqlClient"/>  
  8. <add name="ConnectionString2" connectionString="Persist Security Info=False;  
  9.        User ID=sa;  
  10.        Password=;  
  11.        Initial Catalog=DataBase2;  
  12.        Server=(local);"  
  13.        providerName="System.Data.SqlClient"/>  
  14. </connectionStrings>   


If you write static class methods, you can use the following method to call them:

 
 
  1. 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:

Reference content is as follows:

 
 
  1. <connectionStrings>  
  2. <add name="LocalSqlServer" connectionString=
    "data source=.SQLEXPRESS;Integrated Security=SSPI;  
  3.        AttachDBFilename= DataDirectory aspnetdb.mdf;  
  4.        User Instance=true" providerName="System.Data.SqlClient" />  
  5. </connectionStrings>  

This is why many netizens often ask on the Forum: Why does my program call the SQL EXPRESS database? If your database configuration is incorrect or cannot be enabled, the SQL EXPRESS database is used. The above describes how to obtain the database string using ASP. NET.

  1. Analysis on PageBase and MasterPage of ASP. NET
  2. XML Web Service Method of ASP. NET
  3. Detailed description of ASP. NET environment deployment
  4. Implement ASP. NET globalization
  5. Solve the Problem of ASP. net ajax script errors

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.