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

Source: Internet
Author: User
Tags sql server management

In the root directory of a website developed in ASP. NET, there is a file named web. config. As the name suggests, this is a file configured for the entire website, in XML format.
Here we will talk about the <connectionStrings> section in the file. The <connectionStrings> section configures the strings connected to the database, because ms SQL Server and ASP. NET is a Microsoft product, so ASP. the preferred database for development in. NET is ms SQL Server. This article only discusses the connection strings of ms SQL Server.
In the first case, the local database is used during local development. The following code is used:
Copy codeThe Code is as follows:
<ConnectionStrings>
<Add name = "myConn"
ConnectionString = "Data Source = (LocalDB) \ v11.0; AttachDbFilename = | DataDirectory | \ Movies. mdf; Integrated Security = True" providerName = "System. Data. SqlClient"/>
</ConnectionStrings>

Discussion:
This is the most common method of using database connection strings for Local Development Using PC. Where,
The name attribute refers to the connection string name. This connection string name must be referenced when the database is used in the website. In this example, myConn is used;
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;
The AttachDbFilename attribute specifies the data name and location. | DataDirectory | corresponding to ASP. the system directory App_Data In the. NET Website. This attribute value indicates that it is connected to the directory named Movies. mdf database. The mdf file name indicates that the database requires the services of the SQL Server, but it is an independent database file, you can copy and paste the database without the need to separate the database in the SQL Server Management System (such as SSMS;
Integrated Security = True "indicates integration verification. It is a Windows authentication method. If this attribute and attribute value are available, the user name and password are not required in the connection string;
ProviderName = "System. Data. SqlClient" is the Data provider
This is the most common situation for local development: You can directly use ASP.. NET to create a database (the extension is. mdf), you can also use ASP.. NET website configuration to generate ASPNETDB. MDF database. After the database is created. create a connection string on the. 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 created on the web. in the config file.

In the second case, the following connection string is used during local development:
Copy codeThe Code is as follows:
<ConnectionStrings>
<Add name = "myConn" connectionString = "Data Source = | DataDirectory | MvcMusicStore. sdf"
ProviderName = "System. Data. SqlServerCe.4.0"/>
</ConnectionStrings>

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

In the third case, use the following code during local development:
Copy codeThe Code is as follows:
<ConnectionStrings
<Add name = "DefaultConnection"
ConnectionString = "Data Source = (LocalDb) \ v11.0; Initial Catalog = aspnet-MvcMovie-
2012213181139; Integrated Security = true "providerName =" System. Data. SqlClient "/>
</ConnectionStrings>

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 (that is, if you want to copy the database, the database needs to be detached from the SQL Server management tool ). This kind of connection is often used to create a database using SQL Server and then use ASP. NET to create a connection. It is not very common during local development.

In case 4, the following code is displayed during remote deployment:
Copy codeThe Code is as follows:
<ConnectionStrings>
<Add name = "myConn" connectionString = "Data Source = server name; Initial Catalog = database name; uid = user ID; pwd = user password;"/>
</ConnectionStrings>

Note: ASP.. NET website is deployed to a remote space (such as a rented space or a server). This connection string is used when the Website access service is actually provided. For a rented space, the Space Provider provides the server name, database name, user name, and password. Note that the Integrated Security = true attribute name and attribute value are not available here, so the user name and password are required.
Therefore, when the connection string is used for Local Development (usually the first method in this article) and the test is successful, you need, you can modify the connection string and upload it to the remote server to provide real Internet access.

Related Article

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.