Asp. NET database connection string written in the Config.web configuration file

Source: Internet
Author: User

method One:

//1 Add <configuration> <appSettings> <add key= "sqlconstring" value= "in Web.config conafiguration Server=diablota;database=dbmessage;uid=sa;pwd= "/> </appSettings> <connectionStrings/>//2 New Class library Data.cs inside the sqlconstring static private property reads the configuration file using System.Configuration; public class Data {private data () {} public static string Sqlconstring {get {return configurationmanager.appsettings[] Sqlconstring "]; When//3 is instantiated, the static method calls SqlConnection Sqlcon = new SqlConnection (data.sqlconstring) directly

 

 

Method Two:

 

//1 web.config Add configuration information <configuration> <connectionStrings> <add name= "Constr" Server=localhost;database=master;uid=sa;pwd= "/> </connectionStrings> </configuration>//2 Add Reference// Reference assembly Add Reference-> the->system.configuration//import namespace under. NET System.Configuration//3 get connection string public static string Getconstr () {//Read the configuration file string connectionstringsetting setting = configurationmanager.connectionstrings["Constr"]//or configurationmanager.connectionstrings["Constr"]. ConnectionString; } 

 

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.