Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Jun 2012CPOL3 min read 8.5K   2   1
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)


Written By
Software Developer
United States United States
Born on 86, had my first computer at the age of 8, wrote my first code at the age of 15(pascal), began to study software engineering at 2005 and graduated in 2010. I have a dev blog www.vasilydev.blogspot.com. My real passion is 3D game programming and playing guitar. I've programmed stuff in C#, python, Delphi, PHP, C++, JS, QT and others...

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult26-Jun-12 8:21
mvePIEBALDconsult26-Jun-12 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.