SoFunction
Updated on 2025-03-08

Detailed explanation of C# programming to connect to ACCESS database instance

This article describes the C# programming method to connect to ACCESS database. Share it for your reference, as follows:

1. Create a FORM form, add a button control, and add a DATAGRIDVIEW control.

2. Double-click FORM to add the namespace

Copy the codeThe code is as follows:
using ;

Double-click the button to enter the button code and write the following code

OleDbConnection strConnection = new OleDbConnection("Provider=.4.0;Data Source=" + "Employee information.mdb" + ";Persist Security Info=False");
// Establish a database engine connection, note that the data table (suffixed with .db) should be placed under the DEBUG fileOleDbDataAdapter myda = new OleDbDataAdapter("select * from employee ,strConnection);
//Create an adapter and search the database through SQL statementsDataSet myds = new DataSet();
//Create a data set(myds, "employee");
//Fill the data table that has been connected to the data set MYDS table in the data set by FILL = ["Contact ID"];
//Use display controls to display table

3. After pressing F5 to run, click the BUTTON button to display the table in the database under the corresponding SQL statement.

The following uses Command and reader objects to output data under the console application.

using System;
using ;
using ;
using ;
using ;
namespace ConsoleApplication19
{ 
  class Program
  {
    static void Main(string[] args)
    {
      OleDbConnection mycon =null;
      OleDbDataReader myReader=null;
      try
      {
        string strcon = "Provider=.4.0;Data Source=;";
        mycon = new OleDbConnection(strcon);
        ();
        string sql = "select * from employee";
        OleDbCommand mycom = new OleDbCommand(sql, mycon);
        myReader = ();
        while (())
        {
          ((0)+" "+(1)+" "+(2)+" "+(3)+" "+(4));
        }
      }
      finally
      {
        ();
        ();
      }
    }
  }
}

I hope this article will be helpful to everyone's C# programming.