You will often encounter this situation: on almost every page of the website, some global processing information is stored. The ideal approach is to store this information in a centralized repository once, rather than repeating this operation on every page of the website. For example, the database connection string is such information. If this information is not stored centrally in a specific area, but is manually entered on each page of the website that needs to connect to the database, it can be imagined that when the database connection string changes, it will cause confusion. What a headache, you have to traverse all the pages in the website that are connected to the database to modify it!
In ASP.NET, through Web.config, you can use the <appSettings> tag. In this tag, you can define zero to multiple settings using the <add ... /> tag. In this article we mainly discuss how to use web.config to configure a database connection in a web application.
The web.config file is a standard xml file. We can use it to set settings for each web application or an application on a machine or an asp.net page in a directory. Of course, it can also be used for A separate web page to set up.
For example: the home directory of the website is inetpubwwwroot, then we place web.config under it, then 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>
<appsettings>
<add key="connstring" value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
</configuration>
Here we discuss how to set up the database connection in web.config.
1. Connect to a database:
Add after <configuration> in web.config
<appsettings>
<add key="connstring"
value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
In the program, you can use the following code to use the settings in web.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. The same goes for connecting to multiple databases, that is, using multiple different key values to set
3. Set database links for applications in different subdirectories. This is a very interesting method. Before setting it, let’s explain its purpose:
If there are multiple subdirectories under a virtual directory, the web application under each subdirectory needs to connect to a different database. How to do this? ?
One method is to create a web.config in each subdirectory and use it to set the database connection in this directory. But the problem with this method is that it requires maintaining web.config in each directory.
Method two is to only create a web.config in the virtual directory and set the database connection of the application in each subdirectory in it. Having said this, you will think of the second method above, using multiple different key values to set, this is indeed a method.
Here, what I want to explain is another method: arrange web.config in the virtual directory, use the location tag in it, and use the same key value to connect to the database. The benefits of this are obvious, because using the same key value, As a result, common statements can be used to connect to the database in applications in all directories. This means that when the program is relocated in the future, there is no need to modify the statements in the program to connect to the database.
The specific settings are as follows:
<location path="news">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=news;server=(local)" />
</appsettings>
</location>
<location path="bbs">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=bbs;server=(local)" />
</appsettings>
</location>
<location path="soft">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=soft;server=(local)" />
</appsettings>
</location>
Note: In the above example, news, bbs, and soft are subdirectories under the virtual directory.
When using connections in the program, use the following method:
public function getconnectionstring()
configurationsettings.appsettings().item("connstring"
end sub
One final point to note is that in order to effectively utilize .config files, you should create standard key and value definitions that can be used by all application developers. This allows developers on the same project to adopt common project settings. These standards are useful when deploying applications and turning them into products.
You will often encounter this situation: on almost every page of the website, some global processing information is stored. The ideal approach is to store this information centrally in a repository once, rather than repeating this operation on every page of the website. For example, the database connection string is such information. If this information is not stored centrally in a specific area, but is manually entered on each page of the website that needs to connect to the database, it can be imagined that when the database connection string changes, it will cause confusion. What a headache, you have to traverse all the pages in the website that are connected to the database to modify it!
In ASP.NET, through Web.config, you can use the <appSettings> tag. In this tag, you can define zero to multiple settings using the <add ... /> tag. In this article we mainly discuss how to use web.config to configure a database connection in a web application.
The web.config file is a standard xml file. We can use it to set settings for each web application or an application on a machine or an asp.net page in a directory. Of course, it can also be used for A separate web page to set up.
For example: the home directory of the website is inetpubwwwroot, then we place web.config under it, then 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>
<appsettings>
<add key="connstring" value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
</configuration>
Here we discuss how to set up the database connection in web.config.
1. Connect to a database:
Add after <configuration> in web.config
<appsettings>
<add key="connstring"
value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
In the program, you can use the following code to use the settings in web.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. The same goes for connecting to multiple databases, that is, using multiple different key values to set
3. Set database links for applications in different subdirectories. This is a very interesting method. Before setting it, let’s explain its purpose:
If there are multiple subdirectories under a virtual directory, the web application under each subdirectory needs to connect to a different database. How to do this? ?
One method is to create a web.config in each subdirectory and use it to set the database connection in this directory. But the problem with this method is that it requires maintaining web.config in each directory.
Method two is to only create a web.config in the virtual directory and set the database connection of the application in each subdirectory in it. Having said this, you will think of the second method above, using multiple different key values to set, this is indeed a method.
Here, what I want to explain is another method: arrange web.config in the virtual directory, use the location tag in it, and use the same key value to connect to the database. The benefits of this are obvious, because using the same key value, As a result, common statements can be used to connect to the database in applications in all directories. This means that when the program is relocated in the future, there is no need to modify the statements in the program to connect to the database.
The specific settings are as follows:
<location path="news">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=news;server=(local)" />
</appsettings>
</location>
<location path="bbs">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=bbs;server=(local)" />
</appsettings>
</location>
<location path="soft">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=soft;server=(local)" />
</appsettings>
</location>
Note: In the above example, news, bbs, and soft are subdirectories under the virtual directory.
When using connections in the program, use the following method:
public function getconnectionstring()
configurationsettings.appsettings().item("connstring"
end sub
One final point to note is that in order to effectively utilize .config files, you should create standard key and value definitions that can be used by all application developers. This allows developers on the same project to adopt common project settings. These standards are useful when deploying applications and turning them into products.
You will often encounter this situation: on almost every page of the website, some global processing information is stored. The ideal approach is to store this information centrally in a repository once, rather than repeating this operation on every page of the website. For example, the database connection string is such information. If this information is not stored centrally in a specific area, but is manually entered on each page of the website that needs to connect to the database, it can be imagined that when the database connection string changes, it will cause confusion. What a headache, you have to traverse all the pages in the website that are connected to the database to modify it!
In ASP.NET, through Web.config, you can use the <appSettings> tag. In this tag, you can define zero to multiple settings using the <add ... /> tag. In this article we mainly discuss how to use web.config to configure a database connection in a web application.
The web.config file is a standard xml file. We can use it to set settings for each web application or an application on a machine or an asp.net page in a directory. Of course, it can also be used for A separate web page to set up.
For example: the home directory of the website is inetpubwwwroot, then we place web.config under it, then 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>
<appsettings>
<add key="connstring" value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
</configuration>
Here we discuss how to set up the database connection in web.config.
1. Connect to a database:
Add after <configuration> in web.config
<appsettings>
<add key="connstring"
value="uid=flash;password=3.1415926;database=news;server=(local)" />
</appsettings>
In the program, you can use the following code to use the settings in web.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. The same goes for connecting to multiple databases, that is, using multiple different key values to set
3. Set up database links for applications in different subdirectories. This is a very interesting method. Before setting it up, let’s first explain its purpose:
If there are multiple subdirectories under a virtual directory, the web application under each subdirectory needs to connect to a different database. How to do this? ?
One method is to create a web.config in each subdirectory and use it to set the database connection in this directory. But the problem with this method is that it requires maintaining web.config in each directory.
Method two is to only create a web.config in the virtual directory and set the database connection of the application in each subdirectory in it. Having said this, you will think of the second method above, using multiple different key values to set, which is indeed a method.
Here, what I want to explain is another method: arrange web.config in the virtual directory, use the location tag in it, and use the same key value to connect to the database. The benefits of this are obvious, because using the same key value, As a result, common statements can be used to connect to the database in applications in all directories. This means that when the program is relocated in the future, there is no need to modify the statements in the program to connect to the database.
The specific settings are as follows:
<location path="news">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=news;server=(local)" />
</appsettings>
</location>
<location path="bbs">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=bbs;server=(local)" />
</appsettings>
</location>
<location path="soft">
<appsettings>
<add key="connstring" value="uid=flyangel;password=3.1415926;database=soft;server=(local)" />
</appsettings>
</location>
Note: In the above example, news, bbs, and soft are subdirectories under the virtual directory.
When using connections in the program, use the following method:
public function getconnectionstring()
configurationsettings.appsettings().item("connstring"
end sub
One final point to note is that in order to effectively utilize .config files, you should create standard key and value definitions that can be used by all application developers. This allows developers on the same project to adopt common project settings. These standards are useful when deploying applications and turning them into products.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/yunazhaozile/archive/2009/12/23/5060746.aspx