C # Access database software uses transactions to insert multiple records
protected void Button1_Click (object sender, EventArgs e)
{
/*============= Test Passed ===============*/
OleDbConnection con = new
OleDbConnection ("Provider=microsoft.jet.oledb.4.0;da
Ta
Source=c:/inetpub/wwwroot/dotnetarticle/app_data/dotnetarticle.mdb ");
Con. Open ();
OleDbCommand cmd = new OleDbCommand ();
Cmd. Connection = con;
Cmd. Transaction = con. BeginTransaction ();
Try
{
string sql1 = "INSERT INTO Teaminfo (Num,username,isplay) VALUES (1, ' a ', 1)";
String sql2 = "INSERT INTO Teaminfo (Num,username,isplay) VALUES (2, ' B ', 1)";
String sql3 = "INSERT INTO Teaminfo (Num,username,isplay) VALUES (3, ' C ', 1)";
String[] Sqlstringlist = {SQL1, sql2, sql3};
for (int n = 0; n < sqlstringlist.length; n++)
{
String strSQL = Sqlstringlist[n]. ToString ();
if (strSQL. Trim (). Length > 1)
{
Cmd.commandtext = strSQL;
Cmd. ExecuteNonQuery ();
}
}
Cmd.Transaction.Commit (); Commit a transaction
Response.Write ("OK");
}
catch (Exception)
{
Cmd. Transaction.rollback ();
}
Finally
{
Con. Close ();
}
}