Use web. config in ASP. NET to configure database connections in Web Applications

Source: Internet
Author: User

[Abstract]: saves the database connection configuration information in the web. config file, so that you do not need to re-compile the application.ProgramTo update certain properties of the application. When you want to migrate the database to another server, you only need to modify the web. the database connection configuration information in the config file does not need to be re-compiled or re-deployed to meet the requirements of the new server.

You will often encounter this situation: storing global processing information on almost every page of the website. The ideal practice is to store the information in the archive at one time, instead of repeating this operation on every page of the website. For example, the database connection string is such information. If the information is not stored in a specific area in a centralized manner, it is manually entered on every page on the website that needs to connect to the database, you can imagine: it can be a headache when the database connection string is changed. You must traverse all the pages connecting to the database on the website to modify it!
In ASP.. net, through the Web. config, you can use the <deleettings> tag. In this tag, you can use <add... /> define 0 to multiple settings. This article mainly discusses how to use web. config to configure a database connection in a web application.

Web. the config file is a standard XML file. We can use it to set each web application under a machine, an application, or the Asp.net page under a directory. Of course, it can also be set for a separate web page.

For example, if the main directory of a website is \ Inetpub \ wwwroot \, we place Web. config under it, and the applications in this website will be affected by the settings in Web. config.
E.g .:
<? XML version = "1.0" encoding = "gb2312"?>
<Configuration>
<System. Web>
<Compilation defaultlanguage = "VB" DEBUG = "true"/>
<Customerrors mode = "remoteonly" defaultredirect = "JS/error.htm">
<Error statuscode = "404" Redirect = "JS/filenotfound. aspx"/>
<Error statuscode = "500" Redirect = "JS/error.htm"/>
</Customerrors>
<Authentication mode = "Windows"/>
<Authorization>
<Allow users = "*"/>
</Authorization>
<Httpruntime maxrequestlength = "4000" usefullyqualifiedredirecturl = "true" executiontimeout = "45"/>
<Trace enabled = "false" requestlimit = "10" pageoutput = "false" tracemode = "sortbytime" localonly = "true"/>
<Sessionstate mode = "inproc" stateconnectionstring = "TCPIP = 127.0.0.1: 43444" cookieless = "false" timeout = "20"/>
<Globalization requestencoding = "gb2312" responseencoding = "gb2312" fileencoding = "gb2312"/>
</System. Web>
<Deleetask>
<Add key = "connstring" value = "uid = flash; Password = 3.1415926; database = News; server = (local)"/>
</Appsettings>
</Configuration>

Here we will discuss how to set up database connections in Web. config.

1. connect to a database:
Add <configuration> in Web. config

<Deleetask>
<Add key = "connstring"
Value = "uid = flash; Password = 3.1415926; database = News; server = (local)"/>
</Appsettings>

In the program, you can use the followingCodeTo use the settings in Web. config:

-----Vb.net -----
Imports system. Configuration
Dim myvar as string
Myvar = configurationsettings. receivettings ("connstring"
----- C #-----
Using system. configuration;
String myvar;
Myvar = configurationsettings. deleettings ["connstring"];

2. Connect to multiple databases
Similarly, you can set multiple key values.

3. Set database links for applications in different subdirectories
This is an interesting method. Before setting it, describe its purpose:
If there are multiple subdirectories under a virtual directory and the Web applications under each subdirectory need to connect to different databases, what should we do ??
One way is to create a web. config in each subdirectory and use it to set the database connection under this directory. However, the problem with this method is that the Web. config under each directory needs to be maintained.

Method 2: create a web. config in the virtual directory and set the database connection of the application in each subdirectory. Speaking of this, you will think of the second method above, using multiple different key values to set, this is indeed a method.

Here, I want to explain another method: deploy the web under the virtual directory. config, in which the location tag is used, and the same key value is used to connect to the database. This advantage is obvious because the same key value is used, the common statements can be used in applications in all directories to connect to the database. In this case, when the program is migrated in the future, the statements used to modify the database connection in the program are not used.
The specific settings are as follows:

<Location Path = "news">
<Deleetask>
<Add key = "connstring" value = "uid = flyangel; Password = 3.1415926; database = News; server = (local)"/>
</Appsettings>
</Location>
<Location Path = "BBS">
<Deleetask>
<Add key = "connstring" value = "uid = flyangel; Password = 3.1415926; database = BBS; server = (local)"/>
</Appsettings>
</Location>
<Location Path = "soft">
<Deleetask>
<Add key = "connstring" value = "uid = flyangel; Password = 3.1415926; database = soft; server = (local)"/>
</Appsettings>
</Location>

Note: In the preceding example, news, BBS, and soft are subdirectories in the virtual directory.
Use the following method when using the connection in the program:
Public Function getconnectionstring ()
Configurationsettings. deleettings (). Item ("connstring"
End sub

To effectively use the. config file, you should create standard key names and value definitions for all application developers. In this way, developers of the same project can adopt public project settings. These standards are useful when deploying applications and converting them into products.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.