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. In this article, we
This article mainly discusses how to use web. config to configure a database connection in a web application.
I. Basic usage
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.
Sqlserver connection Configuration:
<Add name = "localsqlserver" connectionstring = "Data Source =./sqlexpress; min pool size = 5; Max pool size = 512; Integrated
Security = true; attachdbfilename = | datadirectory | timetracker. MDF; user instance = true "/>
<Add name = "MDM" connectionstring = "Data Source = 10.21.17.17, 22/nd0; initial catalog = mdm_v2; persist Security info = true; User
Id = sa; Password = Lenovo! 123 "providername =" system. Data. sqlclient "; min pool size = 5; Max pool size = 512;/>
Connectionstring is used when sqlconnectionstringbuilder is instantiated,
For example, sqlconnectionstringbuild builder = new sqlconnectionstringbuild (connectionstring ).
Oracle connection Configuration:
Http://www.connectionstrings.com/oracle
Parameter description:
1. Data Source
The datasource attribute of sqlconnectionstringbuilder corresponds to data source in connectionstring. "Data Source" can be replaced by the following string:
"Server", "Address", "ADDR", and "network address ". Data Source =./sqlexpress can also be written as Data Source = (local)/sqlexpress.
2. Integrated Security
The integratedsecurity attribute of sqlconnectionstringbuilder, which corresponds to the Integrated Security in connectionstring. "Integrated Security" Can
If "trusted_connection" is set to true, the current Windows Account creden。 are used for authentication. If it is set to false, the user ID and password must be specified in the connection.
3. attachdbfilename
The attachdbfilename attribute of sqlconnectionstringbuilder corresponds to attachdbfilename in connectionstring. "attachdbfilename" can be written as "extended properties" and "initial file name ". The attachdbfilename attribute specifies the location of the database files dynamically appended to the server when the connection is opened.
This attribute can accept the full path and relative path of the database (for example, | datadirectory | syntax). This path will be replaced by the app_data directory of the application at runtime.
4. user instance
The userinstance attribute of sqlconnectionstringbuilder corresponds to the user instance in connectionstring. This value indicates whether to redirect the connection from the default SQL Server express instance to the instance that runs under the caller account and starts at runtime. Userinstance = true. In this case, sqlserverexpress
Attach to a new instance, create a new process, and run it under the user identity that opens the connection. In ASP. NET applications, this user is a local ASPNET account or the default
Networkservice, which depends on the operating system. To securely append database files provided by non-system administrator accounts (such as ASP. NET accounts), it is necessary to create an independent sqlserver user instance.
5. Min pool size = 5; Max pool size = 512;
Min pool size for the database connection pool and Max pool size for the maximum number of connections in the database connection pool.
6. What is datadirectory?
Asp.net 2.0 has a special directory app_data. Generally, the SQL Server 2005 express data file is stored in this directory. The corresponding database connection string is:
Connectionstring = "...... Data Source =./sqlexpress; Integrated Security = sspi; attachdbfilename = | datadirectory | data. MDF; User
Instance = true "there is a datadirectory macro. What does it mean?
Datadirectory indicates the replacement string of the database path. Datadirectory simplifies project sharing and application deployment because it does not require hard coding of the complete path. For example, you do not need to use the following connection string:
"Data Source = C:/program files/MyApp/app_data/mydb. MDF"
Use | datadirectory | (included in the following vertical line) to have the following connection string:
"Data Source = | datadirectory |/mydb. MDF ".
It is not only used in SQL Server 2005 Express, but also in other file databases, such as the connection string of sqllite database files:
<Add name = "defaultdb"
Connectionstring = "driverclass = nhib.pdf. Driver. sqlite20driver; dialect = nhib.pdf. dialect. sqlitedialect; Data
Source = | datadirectory |/data. db3 "/>
7. Connect timeout = 9000
Unlike connection objects in other. NET Framework data providers (SQL Server, OLE DB, and ODBC), oracleconnection does not support
Connectiontimeout attribute. Setting connection timeout using properties or connection strings does not work, and the returned value is always zero. Oracle connection does not support databases either.
Attribute or changedatabase method.
Ii. Configuration inheritance
For example, if the Home Directory of the website is/inetpub/wwwroot/, Then we place Web. config under it, and the applications in this website will be affected by the settings in Web. config.
3. Use Configuration
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. receivettings ("connstring ")
----- C #-----
Using system. configuration;
String myvar;
Myvar = configurationsettings. deleettings ["connstring"];
4. 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?
1. 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.
2. only create a web. config in the virtual directory, and set the database connection of the application under each subdirectory in it. 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>
Note: In the preceding example, news and BBS are subdirectories under the virtual directory.
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.