SoFunction
Updated on 2025-03-06

C# operation access database instance code

Essay:

(1) Namespace

            using ;

(2) Connection string

private staticstring connStr = @"Provider= .12.0;Data Source = d:\";

Notice:

a. DataSource = the path to the database (the database is placed in the D disk directory)
b. 2003 version of Access database link string:

 privatestatic stringconnStr =@"Provider = .4.0;DataSource = d:\";
          2007Version ofAccessDatabase link string:privatestaticstring connStr =@"Provider= .12.0;Data Source = d:\";

(3) Establish a connection:

OleDbConnection tempconn =new OleDbConnection(connStr);

(4) Use the OleDbCommand class to execute SQL statements:

 OleDbCommand cmd = new OleDbCommand(sql, tempconn); 
      ();
      ();

Chestnut (tools):

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace test
{
  class AccessHelper
  {
    private static string connStr = @"Provider = .12.0;Data Source = d:\";
    public static OleDbConnection GetConn()
    {
      OleDbConnection tempconn = new OleDbConnection(connStr);
      ();
      ();
      (());
      return (tempconn);
    }
    /// <summary>
    /// Execute the instructions to add, delete, and modify    /// </summary>
    /// <param name="sql">Add, delete, and modify sql statements</param>    /// <param name="param">parameters of sql statement</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static int ExecuteNonQuery(string sql, params OleDbParameter[] param)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        using (OleDbCommand cmd = new OleDbCommand(sql,conn))
        {
          if (param != null)
          {
            (param);
          }
          ();
          return(());
        }
      }
    }
    /// &lt;summary&gt;
    /// Execute the query command to get the value of the first row and first column returned    /// &lt;/summary&gt;
    /// <param name="sql">Query sql statement</param>    /// <param name="param">parameters of sql statement</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static object ExecuteScalar(string sql, params OleDbParameter[] param)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
          if (param != null)
          {
            (param);
          }
          ();
          return (());
        }
      }
    }
    /// &lt;summary&gt;
    /// Execute the query command to get the returned datareader    /// &lt;/summary&gt;
    /// <param name="sql">Query sql statement</param>    /// <param name="param">parameters of sql statement</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param)
    {
      OleDbConnection conn = new OleDbConnection(connStr);
      OleDbCommand cmd = ();
       = sql;
       = ;
      if (param != null)
      {
        (param);
      }
      ();
      return (());
    }
    /// &lt;summary&gt;
    /// Execute the query command to get the datatable    /// &lt;/summary&gt;
    /// <param name="sql">Query sql statement</param>    /// <param name="param">parameters of sql statement</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static DataTable ExecuteDatable(string sql, params OleDbParameter[] param)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
          if (param != null)
          {
            (param);
          }
          DataTable dt = new DataTable();
          OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
          (dt);
          return (dt);
        }
      }
    }
  }
}

Summarize

The above is the example code of the C# operation access database introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!