Access database in app directory

  • Thread starter Thread starter Dinko Deranja
  • Start date Start date
D

Dinko Deranja

My question is simple, but I haven't found an answer:
How to connect to Access database that will always be in the application
root directory (in VS IDE)?

I am making a windows forms application, and I dragged the OleDbConnection
to the form. If I change the conn string (in "WinForm designer generated
code") to ... & Application.StartupPath & "/mydb.mdb.." then whenever I
change something in the form, the above code is automatically replaced (and
hardcoded) with the current value of StartupPath. If I remove the path and
leave only "mydb.mdb" then the default dir is C:\.
 
Hi Dinko,

I have strugled long with this, however when you set the connections string
again to the right place in the load event it does work as far as I know.

Cor
 
Hi Dinko,

Don't touch WinForm designer generated code as you've discovered it is
recreated each time you change something on the form.
Rather, leave ConnectionString empty in designer and set it manually in
override void OnLoaded method:
if (!DesignMode)
{
connectionObject.ConnectionString = ....;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top