How to set the relative path of the web. config access database for asp. net2.0
Today, I happened to be on the web. the problem of configuring connectionStrings in config is that after reading a few books, I only talked about the setting method of SQL, but the access was forgotten to the corner: it was not mentioned ~~
Fortunately, there are some mentioned in the visual studio 2005 help file:
<ConnectionStrings>
<Add name = "MyJetConn" connectionString = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = C: \ testdatasource. mdb; Persist Security Info = False; "providerName =" System. data. oleDb "/>
</ConnectionStrings>
After testing, this setting is acceptable, but it cannot be run on the rented server. What I need is the relative path setting method, although ~ can be used in c ~ The current path is replaced, but after running, it is found that the current path is in windows \ system32.
On the Internet, we found that someone used a special character to replace the relative path, and then used Server. MapPath () to replace the special character When referencing the string in the background code of the page! This is indeed a good method! But it is better that ApplicationDeployment. DataDirectory ---- This property is new in the. NET Framework version 2.0.
The best method:
Web. config Configuration
<Configuration>
<AppSettings/>
<ConnectionStrings>
<Add name = "MyJetConn" connectionString = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = | DataDirectory | \ atm. mdb; Persist Security Info = False; "providerName =" System. data. oleDb "/>
</ConnectionStrings>
...
Test procedure
Public string OpenDB ()
{
String msg;
OleDbConnection MyOleDB = new OleDbConnection ();
MyOleDB. ConnectionString = WebConfigurationManager. ConnectionStrings ["MyJetConn"]. ConnectionString;
Try
{
MyOleDB. Open ();
Msg = "server vision:" + MyOleDB. ServerVersion + "connection is" + MyOleDB. State. ToString ();
Return (msg );
}
Catch (Exception err)
{
Msg = err. Message;
Return (msg );
}
Finally {
MyOleDB. Close ();
}
}
Output:
Server vision: 04.00.20.connection is Open
Note: The website code folder must be at least the root directory of the iis virtual directory. Otherwise, problems may occur during local http debugging.
For example:
Line 26: ASP. NET to identify an incoming user.
Line 27: -->
Line 28: <authentication mode = "Forms"/>
Line 29: <! --
Line 30: The <customErrors> section enables configuration
OK, the problem is solved, and the mood is much better. Everyone can share it!