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
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