Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a window application in C# , with MS-Access database.I have given the following code to connect with my database:

MSIL
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\mydb.mdb";
            OleDbConnection conn = new OleDbConnection((str));

Its running fine. But I want to give only the database file name not the whole path in Data Source.

Thanks.
Posted
Updated 10-May-11 0:52am
v2

keep ur database file in Debug folder of your application then
change ur connectionstring as

C++
string strFilepath = ".\\mydb.mdb";


MSIL
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strFilepath+"";
            
OleDbConnection conn = new OleDbConnection((str));
 
Share this answer
 
Hi udaysama,

You can move your database file to your project folder.

I hope this help,
:)
 
Share this answer
 
Comments
Uday P.Singh 10-May-11 6:51am    
I did what you recommended but, but its giving me an error of could not find file name..
Michael Waguih 10-May-11 6:54am    
It depends on the place you have move it to !!!
Michael Waguih 10-May-11 6:56am    
And be sure you are attaching the database after moving it
For every example I can see on http://www.connectionstrings.com/access[^]

They all seem to use a full path.
 
Share this answer
 
Hi udaysama,

you can use app.config file and add database on your debug folder.

in app.config you can set following tag

appsettings
add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb;Persist Security Info=True ;"

call this code on form load event.

Imports System.Configuration

Public strconnection As String = ConfigurationSettings.AppSettings("ConnectionString")

this is help for you...

Thanks
Mukesh
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900