using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Web;
using System.Data.SqlClient;
namespace 数据回滚
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "/data.mdb");
public void NonReturnDataCommand(params string[] values)
{
OleDbCommand cmd = new OleDbCommand("", conn);
conn.Open();
OleDbTransaction tran = conn.BeginTransaction();
cmd.Transaction = tran;
try
{
foreach (string sql in values)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch(Exception e)
{
tran.Rollback();
MessageBox.Show(e.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
conn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string sqlTran = "insert into B(B1,B2) values(1,555)";
string sqlNoTran = "insert into A(A1,A2) values(2,666)";
NonReturnDataCommand( sqlNoTran,sqlTran);
}
}
}