Connecting to Access Database using C#!


  • Hello Everyone,

    This my first time on here. I am creating an application using C#, I have already created the UI and now I am trying to get the application to connect to an Access Database that I have already created as well. I want to be able to add data to the database using the UI I created. Can anyone help me?

    I have Visual Studio 2010

    • Moved by Peter JausovecMicrosoft employee Thursday, August 05, 2010 5:34 PM moving to more appropriate forum. (From:Sharepoint Development with Visual Studio)
    Tuesday, July 06, 2010 5:26 PM

All replies

  • Welcome to the MSDN Forums.

    Please see a complete example at (How to link to an Access Database in a C# Application)

    Hope this helps enough :)



    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Proposed as answer by Jayant Sharma Thursday, August 18, 2011 11:48 AM
    Monday, August 09, 2010 6:29 AM
  • Hello Mbayo,

        Kindly, find the code below for the same:

    Creating OLE DB Connection Objects in Code:
    You create OleDbConnection objects with the New keyword. You can instantiate the connection and set the connection string in the same call, or you can assign the connection string to the OleDbConnection.ConnectionString property after instantiating the connection. Be sure to replace the data source with a valid path if you are connecting to an Offi ce Access database, or replace the connection string with a valid connection string for the OLE DB data source you want to connect to. To eliminate the need to fully qualify the objects in code, add an Imports System.Data.OleDb statement (Visual Basic) or using System.Data.OleDb; statement
    (C#) to the top of your code fi le.
    // C#
    System.Data.OleDb.OleDbConnection ConnectionToOleDb = new System.Data.OleDb.OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;Data ource=\"Nwind.mdb";Persist Security Info=False");




    Paras Sanghani

    Mark As Answer if it helped you. 


    • Proposed as answer by Jayant Sharma Thursday, August 18, 2011 11:48 AM
    Monday, August 09, 2010 7:01 AM
  • Hello mbayo,

    If u want connection using oledb, may be below code helps u...

    // Example15_2.cs
    using System;
    using System.Data;
    using System.Data.OleDb;
    namespace csbook.ch15 {
      class Example15_2 {
       static void Main(string[] args) {
         string conString =
          + @"data source=c:\data\Northwind.mdb";
         // create an open the connection     
         OleDbConnection conn = new OleDbConnection(conString);
         // create the DataSet
         DataSet ds = new DataSet();
         // create the adapter and fill the DataSet
         OleDbDataAdapter adapter = 
          new OleDbDataAdapter("Select * from Customers", conn);
         // close the connection
         DataTable dt = ds.Tables[0];
         foreach (DataRow dr in dt.Rows) {

    If anything is unclear feel free to ask me.... :)



    • Proposed as answer by Jayant Sharma Thursday, August 18, 2011 11:48 AM
    Monday, August 09, 2010 7:22 AM
  • this is a step by step procedure

    1) open notepad.exe , then simply save as  mdsaputra.udl , don’t forget to change Save as type into All Files (*.*).

    2)Now close your notepad and double click mdsaputra.udl which you just created

    3) under the provider tab, select microsoft office 12.0 access database engine oledb provider

    4) under the connection tab, enter the path to your database e.g C:\Users\BolorunduroWB\Documents\Access\test.mdb

    5) click test connection, if it says test connection successful then you're good. close it and open the mdsaputra.udl file in notepad

    6) copy everything from provider= . . . e.g Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\BolorunduroWB\Documents\Access\test.mdb;Persist Security Info=False

    7) goto your c# file. at the top add these references

    using System.Data;
    using System.Data.OleDb;

    8) goto where in the program you need the database then add

    string connString = @"//place what you copied from the mdsaputra.udl here";
    // e.g  @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\BolorunduroWB\Documents\Access\test.mdb;Persist Security Info=False"
    OleDbConnection conn = new OleDbConnection(connString);

    9) when you need to add data to the tables do this

    OleDbCommand comm = new OleDbCommand("INSERT INTO databasetablename (columnname1 , columnname2 , . . .) VALUES (column1value , column2value ,  . . .)");
    int temp = comm.ExecuteNonQuery();
    if(temp > 1)
                //then the data saved successfully
            //it did not save

    check this for better explanation
    Wednesday, June 05, 2013 10:39 AM