The web site root, developed in ASP.net, has a file named Web.config, which is a file configured for the entire Web site in XML format.
This is mainly about the <connectionStrings> section of the document. The <connectionStrings> section is configured for strings connected to the database, because MS SQL Server and ASP.net are part of Microsoft's product, so the preferred database for using ASP.net development is MS SQL Server, This article only discusses the connection string conditions for MS SQL Server.
In the first case, locally developed, use a local database, such as the following code
Copy Code code 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 use of database connection strings using PCs for local development. 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 the local database server with the version number 11, or SQL Server 2012;
The AttachDbFileName property is to specify the name and location of the specific data, | datadirectory| corresponding to the system catalog App_Data in the ASP.net Web site, this property value describes the Movies.mdf database that is connected to this directory, where the MDF file name indicates that the database requires the service of the SQL Server server, but it is itself a separate database file. can be copied and pasted without the need for database separation in SQL Server management systems (such as SSMS);
Integrated security=true "Description is integrated authentication, is the way of Windows authentication, as long as this property and attribute values, the connection string does not require a username and password;
ProviderName = "System.Data.SqlClient" for data provider
This situation is most commonly used for local development: You can create a database directly using ASP.net (. mdf extension), or you can use the ASP.net web site configuration to build the Aspnetdb.mdf database, and when the database is created, create a connection string in the ASP.net interface, just provide the name of the connection string , other attributes and property values of the connection string can be generated automatically in the Web.config file.
In the second case, when you develop locally, use the following connection string:
Copy Code code as follows:
<connectionStrings>
<add name= "myconn" connectionstring= "Data source=| datadirectory| Mvcmusicstore.sdf "
Providername= "system.data.sqlserverce.4.0"/>
</connectionStrings>
Description: Here the ConnectionString property is simpler than the first, and note the database extension here. sdf, this is the MS SQL Compact version of the database, it does not need to open SQL Server services can be used, exquisite compact, easy to use, But it is less supportive than. MDF (this is the MS SQL Server standard file format). Therefore, when developing on a normal PC, the. mdf file is recommended and, if no MS SQL Server service is running on the machine, the. SDF is a good choice.
In the third case, when you develop locally, use the following code:
Copy Code code 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>
Description: The database name here does not have an extension, it is a database managed directly by MS SQL Server, not a stand-alone database file (that is, if you want to replicate the database, you need to detach the database in the SQL Server Administration tool). This connection is often made using SQL Server to create a database and then create a connection with ASP.net, which is not common when developed locally.
In the fourth case, when you deploy remotely, you see the following code:
Copy Code code as follows:
<connectionStrings>
<add name= "myconn" connectionstring= "data source= server name; Initial catalog= database name; uid= user id;pwd= user password;" />
</connectionStrings>
Note: Use this connection string when you need to deploy a locally developed ASP.net web site to a remote space (such as a rented space, such as your own server) and begin to actually provide the site access service. In the case of leased space, the space provider provides the server name, database name, username and password. Note that there is no integrated security=true attribute name and attribute value, so you need a username and password.
Therefore, when a local development using a connection string, often the first in this article, is completed and tested successfully, the connection string needs to be modified and uploaded to the remote server based on the data from the remote server to provide true Internet access.