At the Web site root of ASP, there is a file called Web. config, as the name implies, which is a file configured for the entire Web site in XML format.
Here is the main discussion of the <connectionStrings> section in the document. The <connectionStrings> section is configured for strings connected to the database, because MS SQL Server and ASP. NET are Microsoft's products, so the database that is preferred when using ASP. This article discusses only the connection string condition for MS SQL Server.
In the first case, when developing locally, use the local database, as in the following code
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>
Discuss:
This is the most common way to use the database connection string for local development using a PC. which
The Name property refers to the connection string names, which need to be referenced when using the database in the Web site; In this case, myconn;
The data source property is the database server, (LocalDB) \v11.0 instructions use the local database server, the version number is 11, which is SQL Server 2012;
The AttachDbFileName property specifies the name and location of the specific data, | datadirectory| corresponding to the system directory in the ASP. App_Data, this property value describes the connection to this directory named Movies.mdf database, where the MDF file name indicates that the database requires a SQL Server Server service, but it is itself a separate database file. You can copy and paste without the need for database separation work in the SQL Server management system (such as SSMS);
Integrated security=true "Description is integrated authentication, is the way of Windows authentication, as long as there is this attribute and attribute value, the connection string does not need the user name and password;
ProviderName = "System.Data.SqlClient" is a data provider
This is the most common scenario for local development: You can create a database directly with ASP. NET (. mdf extension), or you can use the ASP. Aspnetdb.mdf database to be generated when the database is created, creating a connection string in the ASP. Only the name of the connection string is required , other properties and property values of the connection string can be automatically generated in the Web. config file.
In the second case, when developing locally, use the following connection string:
Copy CodeThe code is as follows:
<connectionStrings>
<add name= "myconn" connectionstring= "Data source=| datadirectory| Mvcmusicstore.sdf "
Providername= "system.data.sqlserverce.4.0"/>
</connectionStrings>
Note: Here the ConnectionString property is simpler than the first case, note the database extension here. sdf, this is the MS SQL Compact version of the database, it does not need to open the SQL Server service can be used, compact, easy to use, But the support is inferior to. mdf (This is the MS SQL Server Standard Edition file format). Therefore, when developing a normal PC, it is recommended to use an. mdf file if the. sdf is a good choice when the MS SQL Server service is not running on the machine.
In the third case, when developing locally, use the following code:
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 does not have an extension, stating that it is a database that is managed directly by MS SQL Server, not a separate database file (that is, if you want to replicate the database, you need to detach the database in SQL Server Administration tools). This connection is often used to create a database using SQL Server, and then to create a connection with ASP. Local development is not very common.
In the fourth scenario, when you deploy remotely, you see the following code:
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>
Description: Use this connection string when you need to deploy a locally-developed ASP. NET Web site to remote space, such as a leased space, such as your own server, when you start to actually provide access to your Web site. In the case of leased space, the space provider provides the server name, database name, user name, and password. Note that there is no integrated security=true "attribute name and attribute value, so you need a username and password.
Therefore, when you use the connection string for local development (often the first way in this article) to complete and test successfully, you need to modify the connection string and then upload it to the remote server to provide true Internet access based on the data of the remote server.
How to configure the database connection string connectionstrings section in ASP.