Search code examples
c#ms-accessado.netoledb

Connectivity between access database and c#


I was checking about access database connectivity with c# in social of MSDN where i found the sample connection string as follows

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";

Now my question is suppose i have created a form application and i put the database file abc.mdb at the same location where the .exe file resides. In that case can i write the connection string as follows?

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=abc.mdb;Jet OLEDB:Database Password=password";

I was trying this with SQL database file but it was not running may be the full path is mandatory for this case. I'm i right?


Solution

  • If you can't use a relative path in connection string, you can generate it at runtime something like:

    string connstring = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=true", Path.Combine(Directory.GetCurrentDirectory(), "MyDatabase01.accdb"));