ASP.NET Configuring database connections in web applications in web.net

xiaoxiao2021-03-06  93

You will often encounter this situation: On each page of the website, you will store some global processing information. The ideal approach is to store this information in a one-time concentration in the data archive, rather than repeating such an operation on each page of the website. For example, the database connection string is such information, if this information is not centrally stored in a specific area, but is manually input on the page of the website, it is possible to enlance: When the database connection string is changed, it will be Headache, you must traverse all the pages of all connected databases in the website to modify!

In ASP.NET, through Web.config, you can use tags in this tag, you can define 0 to multiple settings with tag. In this article we mainly discuss how to use Web.config to configure database connections in a web application.

Web.config files are standard XML files, we can use it to set up for each web application under a machine or an ASP.NET page in an application or a directory, of course, it can also be A separate web page for settings.

Such as: The main directory of the website is / inetpub / wwwroot /, then we put the web.config under this, then the application in this website will be affected by the settings in Web.config. EG: < globalization requestencoding = "gb2312" responseencoding = "gb2312" fileencoding = "gb2312" /> We discuss how Setting the database connection in web.config.

1. Connect a database: Add to in Web.config

In the program, you can use the following code to use the web. Settings in Config:

----- VB.NET ----- Imports System.configuration Dim MyVar As String Myvar = ConfigurationSettings.appsettings ("Connstring" ----- C # ----- Using System.configuration; String Myvar; MyVar = ConfigurationSettings.AppSettings ["connString"];

2, connect multiple databases, which is to use multiple different Key values ​​to set

3, set the database link of the application in different subdirectory This is a very interesting way, before setting, first explain its use: If there are multiple subdirectories in a virtual directory, each subdirectory under the Web How do I do connection with different databases? ? One method is to establish a web.config in each subdirectory, use it to set the database connection in this directory. But the problem with this method is to maintain the Web.config in each directory.

Method 2 is to establish a web.config in a virtual directory, set up a database connection of the application in each subdirectory. Speaking here, you will think of the second method above, use multiple different Key values ​​to set, this is indeed a way.

Here, I want to explain another method: laying web.config in a virtual directory, using the location tag, use the same key value to connect to the database, this is obvious because of the same key value, In applications in all directories, a common statement can be used to connect to the database, which occurs when the program is migrated, and the statement to connect to the database is not used. The specific settings are as follows:

< Location path = "bbs">

Finally, it is desirable to use the .config file effectively, you should create a standard key name and value definition for all application developers. This allows developers of the same project to use public project settings. These standards are very useful when deploying applications and transforming them into products.

转载请注明原文地址:https://www.9cbs.com/read-122747.html

New Post(0)