Home > Backend Development > C#.Net Tutorial > How to configure the database connection string connectionStrings section in ASP.NET web.config

How to configure the database connection string connectionStrings section in ASP.NET web.config

高洛峰
Release: 2017-01-13 13:21:35
Original
1366 people have browsed it

In the root directory of the website developed by ASP.NET, there is a file named web.config. As the name suggests, this is a file that configures the entire website, and its format is XML format.
Here we mainly talk about the section in the file. The section configures the strings connected to the database. Since MS SQL Server and ASP.NET are both Microsoft products, the preferred database when developing using ASP.NET is MS SQL Server. This article only discusses the MS SQL Server connection string situation.
In the first case, when developing locally, use a local database, such as the following code

<connectionStrings>
    <add name="myConn"
connectionString ="Data Source=(LocalDB) \v11.0;AttachDbFilename=|DataDirectory| \Movies.mdf;Integrated Security=True" providerName ="System.Data.SqlClient" />
</connectionStrings>
Copy after login

Discussion:
This is the most common way to use the database connection string for local development using a PC. Among them, the
name attribute refers to the connection string name. When a database needs to be used on the website, this connection string name needs to be quoted; in this example, it is myConn; the
Data Source attribute is the database server, (LocalDB) \V11.0 indicates that the local database server is used, the version number is 11, that is, SQL Server 2012;
AttachDbFilename attribute specifies the specific data as the name and location, |DataDirectory| corresponds to the system directory App_Data in the ASP.NET website, this attribute The value description connects to the database named Movies.mdf in this directory. The mdf file name indicates that the database requires the service of the SQL Server server, but it is an independent database file itself and can be copied and pasted without the need for SQL Server. The management system (such as SSMS) performs database separation work;
Integrated Security=True" indicates integrated authentication, which is the Windows authentication method. As long as this attribute and attribute value are present, the user name and password are not required in the connection string;
providerName="System.Data.SqlClient" is the data provider
This situation is the most common situation for local development: you can directly use ASP.NET to create a database (the extension is .mdf), or you can use The ASP.NET website configuration generates the ASPNETDB.MDF database. When the database is created, create a connection string in the ASP.NET interface. You only need to provide the name of the connection string. Other attributes and attribute values ​​​​of the connection string can be automatically added to the web .config file.

In the second case, when developing locally, use the following connection string:

<connectionStrings>
    <add name="myConn" connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Copy after login

Note: The connectionString attribute here is simpler than the first case. Pay attention here. The database extension is .sdf, which is the MS SQL Compact version of the database. It can be used without opening the SQL Server service. It is compact and easy to use, but its support is not as good as .mdf (this is the MS SQL Server standard version file format ). Therefore, it is recommended to use .mdf files when developing on a normal PC. If there is no MS SQL Server service running on the machine, .sdf is a good choice.

The third situation. When developing locally, use the following code:

<connectionStrings
<add name="DefaultConnection"
connectionString ="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-
2012213181139;Integrated Security=true" providerName ="System.Data.SqlClient" />
</connectionStrings>
Copy after login

Note: The database name here has no extension, indicating that it is a database directly managed by MS SQL Server, not an independent database file (ie: if you want to copy the database, This database needs to be detached in the SQL Server management tool). This kind of connection often uses SQL Server to create the database, and then uses ASP.NET to create the connection. It is not very common when developing locally.

In the fourth case, when deploying remotely, you can see the following code:

<connectionStrings>
    <add name="myConn" connectionString="Data Source=服务器名;Initial Catalog=数据库名;uid=用户Id;pwd=用户密码;"/>
</connectionStrings>
Copy after login
Description: When you need to deploy the locally developed ASP.NET website to a remote space (such as renting space, such as your own server), this connection string is used when actually providing website access services. If you rent space, the space provider will provide the server name, database name, user name and password. Note that there is no Integrated Security=true" attribute name and attribute value, so a username and password are required.

Therefore, when using the connection string for local development (often the first method in this article) is completed After the test is successful, the connection string needs to be modified according to the data of the remote server and then uploaded to the remote server to provide real Internet access.

More database connection strings in ASP.NET web.config. For related articles on the configuration method of the connectionStrings section, please pay attention to the PHP Chinese website

!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template