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
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>
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>
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>
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>
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
!