Two ways to configure a database (majority) connection in Web. config

Source: Internet
Author: User
Tags connectionstrings

This is my first article, since it is the first article, then from the most basic just talk about the configuration database connection in--web.config.

There is a lot of information on the Internet, but found that there is no one from the beginning in place very clear to say, today put my collation here.

In the development of Web site, database operation is a frequently used operation, ASP. NET is the general practice of configuring the Database connection code in Web. config and then invoking the database connection code in the program, the benefit is that when the database connection code needs to be changed, we just modify the database connection code in Web. config without having to modify the database connection code in each page.

There are two ways to configure database connection code in ASP. AppSettings and ConnectionStrings, respectively. When you use AppSettings and connectionStrings to configure the database connection code, you can add the following code, respectively, under <configuration>:

1. appSettings

<appSettings>
<add key= "conn1" value= "server= server name; database= database name; uid= user name; password= password;" />
<!--If you need to configure multiple direct additions, you can get the connection by key--
    <add key= "conn2" value= "server= server name; database= database name; uid= user name; password= password;" />
</appSettings>
or create a different configuration file, just cite it in Webconfig, example
<appsettings configsource= "App_data\testconfig.config" ></appSettings>
If the cubby less words directly in this page configuration is good. When connecting to a library for a long time, it is recommended to use the second one, which appears to be more clear than (the individual thinks that there is no difference in functionality).

2. ConnectionStrings

<connectionStrings>
<add name= "conn1" connectionstring= "dserver= server name; database= database name; uid= user name; password= password" providername= " System.Data.SqlClient "/> Note here that appsettings does not need to write a namespace when connecting
<!--If you need to configure multiple direct additions, the connection is obtained by name--
    <add name= "conn2" connectionstring= "dserver= server name; database= database name; uid= user name; password= password" providername= " System.Data.SqlClient "/>
</connectionStrings>
Of course, since appsettings can reference external, corresponding connectionstrings can also, example
<connectionstrings configsource= "External file path" >

The difference between appSettings and connectionStrings: (Excerpt from http://www.cnblogs.com/kerry1986/archive/2009/07/08/1518895.html)

(1) appSettings is commonly used in 2003, ConnectionStrings is commonly used in 2005;

(2) Benefits of using connectionStrings:

First, the connection string can be encrypted, using an encryption tool of MS;

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

Third, the database platform can be easily replaced, such as the Oracle database, only need to modify the providerName.

(3) write in appSettings with system.configuration.configurationmanager.appsettings["KeyName"] get the database connection code value;

Write in connectionStrings with system.configuration.configurationmanager.connectionstrings["name" to get the database connection code value.

It is said that the two are common, but the second is the new feature of Asp.net2.0, the first is recommended.

In fact, I always have a question, two uid;pwd in a string, and the user ID; Password is equivalent.

According to the information I found on the internet can be interchangeable universal. A seemingly more detailed explanation is found on the Web: the mechanism for connecting to a SQL Server database is not much different from the mechanism for connecting to access, except for changing the parameters in the Connection object and the connection string.

First, the namespaces used to connect to SQL Server are not "System.Data.OleDb", but "System.Data.SqlClient".

Next is his connection string, we introduce a parameter (note: The parameters are separated by semicolons):

"User Id=sa": The authenticated user name for the connection database is SA. He also has an alias "UID", so this sentence we can also write "Uid=sa". "Password=": The authentication password for the connection database is empty. His nickname is "pwd", so we can write "pwd=".

Note here that your SQL Server must already have a username and password to log in, or you cannot log in in such a way. If your SQL Server is set to Windows logon, you do not need to use "User ID" and "password" here. Such a way to log in, and you need to use "TRUSTED_CONNECTION=SSPI" to log in.

"Initial Catalog=northwind": The data source used is the database "Northwind". His nickname is "database", this sentence can be written as "Database=northwind".

"Server=yoursqlserver": Use a server named "YourSQLServer". His nickname is "Data Source", "Address", "Addr".

If you are using a local database and you have defined the instance name, you can write as "server= (local) \ Instance name" or "(local)" to the name or IP address of the remote server if it is a remote server.

"Connect timeout=30": The connection time-out is 30 seconds.

Two ways to configure a database (majority) connection in Web. config

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.