C # process Access transactions

Source: Internet
Author: User
Access cannot directly execute multiple statements like SQL server, but it can still be executed together to bind multiple statements into transactions.

The so-called transaction is to treat multiple things as one thing. That is, everyone is on the same ship!
One transaction is used to synchronize multiple tables, either successfully or not. The following example uses C # To handle Access database transactions:

Submit data to one table and update the data in the other table.

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. OleDb;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

}
Protected void button#click (object sender, EventArgs e)
{
String id = "";
String strCon = System. Configuration. ConfigurationManager. etettings ["ConnectStr"]. ToString ();
OleDbConnection con = new OleDbConnection (strCon );
OleDbDataAdapter adp = new OleDbDataAdapter ();
OleDbDataAdapter adp1 = new OleDbDataAdapter ();
Try
{
Con. Open ();
OleDbTransaction tra = con. BeginTransaction (); // create a transaction and start executing the transaction
Adp = new OleDbDataAdapter ("select * from sequence number table", con );
Adp. SelectCommand. Transaction = tra;
Adp1 = new OleDbDataAdapter ("select * from ", con );
Adp1.SelectCommand. Transaction = tra;

OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder (adp );
OleDbCommandBuilder thisBuilder1 = new OleDbCommandBuilder (adp1 );

DataSet ds = new DataSet ();

Adp. Fill (ds, "aa"); // Add a dataset
Id = ds. Tables ["aa"]. Rows [0] [1]. ToString ();
Int64 s = 0;
S = Convert. ToInt64 (id) + 1;
Id = s. ToString ("0000000 #");
Ds. Tables ["aa"]. Rows [0] [1] = id;

Adp. Update (ds, "aa"); // execute the transaction for modifying a table
Adp1.Fill (ds, "bb ");
DataRow dr = ds. Tables ["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 ";

Ds. Tables ["bb"]. Rows. Add (dr );
Adp1.Update (ds, "bb ");

Tra. Commit (); // close the transaction
}
Catch (Exception ex)
{

}
Finally
{
Con. Close ();

}

}

 

Note: Access transactions do not support automatic locking (confirmed by experiments). Therefore, it is best to use Access for local programs. Do not use Access in B/s unless you do not need to process transactions ~~!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.