SoFunction
Updated on 2025-03-07

How to handle transactions in Access in C#

This article describes the method of C# processing transactions in Access. Share it for your reference. The details are as follows:

Access cannot directly execute multiple statements like SQL server, but it can still be executed together by binding multiple statements into transactions. The so-called transaction is to treat multiple things as one thing. That is, everyone is on the same boat! A transaction completes the synchronization operation of multiple tables, either all of which are successful or non-successful. Here is an example, using C# to implement the processing method of Access database transactions: Submit data to one table and update the data in another table at the same time

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class _Default :  
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    string id = "";
    string strCon = ["ConnectStr"].ToString();
    OleDbConnection con = new OleDbConnection(strCon);   
    OleDbDataAdapter adp = new OleDbDataAdapter(); 
    OleDbDataAdapter adp1 = new OleDbDataAdapter();
    try
    {
      ();
      OleDbTransaction tra = (); //Create a transaction and start executing the transaction      adp = new OleDbDataAdapter("select * from serial number table", con);
       = tra;
      adp1=new OleDbDataAdapter("select * from program list", con);
       = tra;
      OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(adp);  
      OleDbCommandBuilder thisBuilder1 = new OleDbCommandBuilder(adp1); 
      DataSet ds = new DataSet();
      (ds,"aa");//Add dataset      id = ["aa"].Rows[0][1].ToString();
      Int64 s = 0;
      s = Convert.ToInt64(id) + 1;
      id = ("0000000#");
      ["aa"].Rows[0][1] = id; 
      (ds,"aa");//Execute transactions that modify a table      (ds,"bb");
      DataRow dr=["bb"].NewRow();
      dr["ProID"]=id;
      dr["ProName"]="ProName";
      dr["ProTime"]="2";
      dr["ProIsFinish"]="3";
      dr["ProBgColor"]="4";
      dr["ProBgPic"]="5";
      dr["ProStyle"]="6";
      dr["MissionName"]="7";
      dr["ProDescription"]="8";
      ["bb"].(dr);
      (ds,"bb");
      ();//Close the transaction    }
    catch (Exception ex)
    {
    }
    finally
    {
      ();
    }
}

Note: Access transactions do not support automatic locking (tested by trials), so Access is best used in native programs. Do not use them in b/s unless you do not use transaction processing~~!

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