ASP. NET web. config database connection string connectionStrings section configuration method

  • 2020-06-07 04:22:22
  • OfStack

In the root directory of the site developed by ASP. NET, there is a file called web. config, which, as the name implies, is configured for the entire site in XML format.
I'm going to focus here on the document < connectionStrings > Section. < connectionStrings > Since MS SQL Server and ES15en.NET are both Microsoft products, MS SQL Server is the preferred database for ASP.NET development. This article only discusses the connection string of MS SQL Server.
In the first case, a local database is used when developing locally, as shown in 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 use of database connection strings for local development using PC. Among them,
The name attribute refers to the connection string name, which should be referenced whenever the database is used in the website. In this case, myConn;
Data Source attribute is database server, (LocalDB)\V11.0 indicates the use of local database server, version number is 11, namely SQL Server 2012;
AttachDbFilename attribute is to specify the specific data as name and location, |DataDirectory| corresponds to App_Data in the system directory of ASP.NET website, this attribute value indicates to connect to the database named Movies.mdf, in which, mdf file name indicates that the database needs the service of SQL Server server, but it is an independent database file. Copy and paste can be done without database separation in SQL Server management system, such as SSMS;
Integrated Security=True" indicates integration verification, which is the method of Windows authentication. As long as there is this attribute and its value, the user name and password are not required in the connection string.
providerName =" System. Data. SqlClient" is the data provider
This kind of situation is the most commonly used local development situation: can be used directly ASP. NET create database (known as extension. mdf), you can also use ASP. NET site configuration generated ASPNETDB. MDF database, after the completion of the database is created, and in ASP. NET interface to create a connection string, only need to provide the name of the connection string, the connection string of other attributes and attribute values can be automatically in web. config file generated.

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

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

connectionString is easier than the first case, note the database extension here.sdf, this is MS SQL Compact version of the database, it does not need to open SQL Server service can be used, delicate and small, easy to use, but not as supportive.mdf (this is MS SQL Server standard version file format). Therefore, it is recommended to use the.mdf file when developing on a normal PC machine. If there is no MS SQL Server service running on the machine,.sdf is a good choice.

In the third case, when developing locally, the following code is used:

<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 directly managed by MS SQL Server and not a separate database file (i.e. if you want to copy the database, you need to separate the database in the SQL Server administration tool). These connections tend to be created using SQL Server to create the database and then ASP.NET to create the connection, which is not very common in local development.

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

<connectionStrings>
    <add name="myConn" connectionString="Data Source= The server name ;Initial Catalog= The database name ;uid= The user Id;pwd= The user password ;"/>
</connectionStrings>

Note: This connection string is used when a locally developed ES119en.NET site needs to be deployed to a remote space (such as rented space, such as your own server) to actually provide access to the site. If you are renting 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 value, so a username and password are required.
Therefore, after local development using the connection string (often the first approach in this article) has been done and successfully tested, the connection string needs to be modified based on the remote server's data and uploaded to the remote server to provide true Internet access.

Related articles: