How to set the relative path of the web. config access database for asp. net2.0

Source: Internet
Author: User
Tags connectionstrings
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!

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.