Two Methods for configuring database connection in Web. config

Source: Internet
Author: User

In website development, database operations are frequently used. In ASP. NET, you can configure database connections in Web. config.CodeAnd thenProgramWhen the database connection code needs to be changed, we only need to modify the web. the database connection code in config does not need to modify the database connection code on each page.

There are two methods to configure the database connection code in ASP. NET:AppsettingsAndConnectionstrings. You can add the following code under <configuration> When configuring the database connection code using appsettings and connectionstrings:

1. appsettings

<Appsettings>
<AddKey= "Conn"Value= "Server = server name; database = database name; uid = user name; Password = password ;"/>
</Appsettings>

2. connectionstrings

<Connectionstrings>
<AddName= "Conn"Connectionstring= "Dserver = server name; database = database name; uid = user name; Password = password"Providername= "System. Data. sqlclient" />
</Connectionstrings>

Appsettings and connectionstrings :( from http://www.cnblogs.com/kerry1986/archive/2009/07/08/1518895.html)

(1) appsettings are commonly used in 2003 and connectionstrings are commonly used in 2005;

(2) advantages of using connectionstrings:

First, you can encrypt the connection string by using an encryption tool of Ms;

Second, you can directly bind a data source control without having to write code to read it and then assign it to the control;

Third, you can easily change the database platform. For example, if you change to an Oracle database, you only need to modify the providername.

(3) Use System in the deleettings. configuration. configurationmanager. appsettings ["keyname"] to obtain the database connection code value. It is written in connectionstrings and used in system. configuration. configurationmanager. connectionstrings ["name"] gets the database connection code value.

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.