How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »


Creating and connecting to a Microsoft Access Database programmatically in C#

Microsoft Access icon

The following example aims to get you a bit more familiar as to how to create and connect to a Microsoft Access database programmatically while being able to add any type of tables you want as well are inserting and retrieving data from it.

Firstly we will need to reference two libraries in our project. Microsoft ActiveX Data Objects 2.0 Library (Interop.ADODB.dll) which we will use to ensure that the connection to the database is closed after we are done creating it, and Microsoft ADO Ext. 2.8 for DDL and Security (Interop.ADOX.dll) which is needed to be able to access the classes that are required to create our database.

After you have finished adding those two references to your project you can use the code sample below to create the database file programmatically. Feel free to modify the code to suit your needs. Also please note that setting your project to Any CPU build will cause exceptions to be thrown when creating the database.

Simply call CreateAccessDatabase(); to create a new database file in your application directory with the name mydb.mdb. You can tweak the method quite a bit if you want to provide the filename as a parameter to the method etc.

The two following snippets will allow you to add new users in the database (based on the table we created above) as well as retrieve data from it. This will hopefully be useful in giving you an idea how to insert and retrieve data from your newly made database.

Inserting data into your database:

Usage example:

Retrieving data from your database:
Keep in mind this is merely an example so this will only return the first row. If you have users in the database with the same name then you will need to use the OleDbDataReader class in order to read all the rows that your query will return.

Usage example:

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

12 Responses to Creating and connecting to a Microsoft Access Database programmatically in C#

  1. Abdullah Yolcu says:

    Thank you so much :)

  2. Humberto Roche says:

    Please on the Solution Explorer set the References\ADOX at the Embed Interop Types from True to False in case you have errors at the ColumnClass. This is what happened to me using Visual Studio 2012.

  3. Patel says:

    Thank You So Much! Really helpful.

  4. sunil says:

    Is it Possible to give path for database
    Thank You.

  5. CooLMinE says:

    Since there were a few requests for a sample project, I’ve created one so you can take a closer look. I took the initiative of using the newer version of the files since one and a half year passed since this post was published. You can always reference the older versions if needed, I’ve tested both and it works exactly the same.

    You can download the project at

    Keep in mind that you will need to recompile it as I’ve removed everything under /bin and /obj folders.

    Let me know if you need further information.

  6. I also would really appreciate a copy of the project.

  7. Sanjarbeck says:

    Can you send me project code please? i needs like this project! Beforly Thanks a lot!

  8. Jason Wee says:

    Can you send me the project files? I believe this will help me in doing my project.

    I am new to C#. Appreciate your help. Thanks

    <email removed for privacy reasons>

    Best Regards,
    Jason Wee

  9. boby says:

    many thank’s.. it’s very helpfully.. search 2 days for completed this task..
    thank you very much..

Leave a Reply

Your email address will not be published. Required fields are marked *