954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Relative path of MS Access Database

Everytime I connect my Access database into my page for some SQL statements like SELECT, INSERT, UPDATE, and DELETE - I make use of an absolute path in my data source such as Data Source = C:\MyData\grades.mdb.

But when I make use of a relative path in my data source such as ~\MyData\grades.mdb, I always receive an OleDbException error message.

Am I doing it correctly? What should be the correct code to have a relative path to the Access Database?

ebabes
Junior Poster in Training
73 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Just a follow up, I am using C# as my script. I make use of OleDbConnection and OleDbCommand to facilitate my SQL transactions. How can I make use of relative path in MS Access?

ebabes
Junior Poster in Training
73 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Server.MapPath("~\MyData\grades.mdb")

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

hi ebabes,
I think problem exists in your connection string.Since you are using C# as your coding language then due to Escape sequence ('\') problem your connection string is not proper.
It should be like that.

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~\\MyData\\grades.mdb");

OR

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\\MyData\\grades.mdb";

Hope this will help you.
Thanks & Regards
Dilipv

dilipv
Light Poster
30 posts since Feb 2008
Reputation Points: 10
Solved Threads: 4
 

I tried the given code this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~\\MyData\\grades.mdb"); but I received an error message "The name Server does not exist in the current context".

Any remedy to this? Thanks.

ebabes
Junior Poster in Training
73 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Here:

this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("\MyData\grades.mdb");

You need to make sure that MyData is a folder in the root directory. This will specify the server's path to the root folder, then on from there is what you specified.

Example:
Let's say your server's physical path is: H:\inetpub\root\

By saying Server.MapPath, it will grab the above line. Then anything within the parenthesis will be added along side the server.mappath:
Server.MapPath("\MyData\grades.mdb")
=
H:\inetpub\root\MyData\grades.mdb

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

Thanks. I don't get any problem about the directory where I put my database. I only find a problem in the word "Server" because it is not accepted by the Web Developer when I insert the code in a class.

I wonder the code above is accepted when I insert it in a click event such as button click. But when I put the code else where in my class, there I receive the message "The word server does not exist in the current context".

ebabes
Junior Poster in Training
73 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You