Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A Few Tips on How to Connect and Work with an Access Database in MS Visual Studio

0.00/5 (No votes)
25 Jun 2012CPOL3 min read 8.6K  
A list of the problems I had to address and how I solved them
Listed here are a few problems I had to address when trying to connect and work with an Access database in MS Visual Studio. The solutions to the problems are also given.

Introduction

Some days ago, I had to make software for a client who said that a local MS Access database was a must. Then I spent a few days researching on the subject, but no matter what, I couldn’t get the database to connect. After a few head bangs, I managed to successfully connect the database to my project discovering the whole topic was a lot easier than you may think. Here, I give you a list of the problems I had to address and how I solved them.

I just wanted an option that allowed me to save data and not overwrite it but also with the option to reflect changes I made to the project database. Then I had to make a little hack, and this hack was the motivation for me to write this tip.

The trick is in development time, connect to the project database not a database located in an output folder. For that hack, you have to modify the connection string like this:

C#
static public void Configure()
{
    string connectionString = 
       "Provider=Microsoft.Jet.OLEDB.4.0;
    Data Source=..\\..\\database.mdb;Persist Security Info=True";
    uta.Connection.ConnectionString = connectionString;
    tdta.Connection.ConnectionString = connectionString;
    pta.Connection.ConnectionString = connectionString;
    abta.Connection.ConnectionString = connectionString;
    sta.Connection.ConnectionString = connectionString;
    saleTa.Connection.ConnectionString = connectionString;
    spta.Connection.ConnectionString = connectionString;
    pota.Connection.ConnectionString = connectionString;
    tabPta.Connection.ConnectionString = connectionString;
}

And make all your project datatables have it. That function must be called before making any query to the database and has to be called only once.

Then anytime you want to copy the output project so it works on other PCs, you only have to comment that line and that is all.

  • The first problem was about the connection, after reading a few forums about connection strings, ODBC access, OLE access, making connection files, all of that was useless for me. Then I discovered that I could drag the database to my project and… voila! It was successfully configured.
  • Another problem I had is that I couldn’t make any query because it said that the MS.jet driver was not installed on this PC. Later after reading some forums, I found out that I had to switch my platform target on the project build options from “Any CPU” to “X86” - that was because Microsoft hasn’t implemented OLEDB 64 bit drivers yet.
  • Having the possibility to perform queries on the database, I couldn’t make the data to be persistent, that means anytime I compiled the software, the database data was lost. That problem is because any time you compile the database on the executable folder was overwritten by the project database that was in the project root folder and that was the database Visual Studio opened to configure. After checking the possible configurations, I discovered that all were wrong for my project, for example:
    1. Copy always: was the default option and overwrites the saved data
    2. Do not copy: didn’t overwrite saved data but changes on datatables and database weren’t reflected on the local database
    3. Copy if newer: that option also overwrote my data, just anytime I saved data to the database.
  • The last problem I faced was that anytime I added a new field on the Access database, I didn’t know how to update it on the datatables and I had to erase the entire database and drag the new one to the project, again losing all the custom queries I had made. The solution was to right click on the selected datatable and select the configure option.

That’s all for now. I wrote this article in the exact number of words I wish I had read on the internet before I began to work on this subject. Any feedback is welcome.

History

  • 25th June, 2012: Initial version

License

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