c#事务回滚!
protected void btnUp_Click(object sender, EventArgs e){
bool b = Upload(FileUpload1);
if (!b)
{
return;
}
string name = FileUpload1.FileName;
string filepath = Server.MapPath("~/upload/") + name;
Pf.CheckTable(Ds, "datetable");
Ds = Db.GotData1("select * from [" + Db.DtName(filepath).Rows[0][2].ToString() + "]", "datetable1", Ds, filepath);
OleDbConnection conn = Db.OpenConn();
OleDbCommand oledbInsertCommand = conn.CreateCommand();
oledbInsertCommand.Transaction = conn.BeginTransaction();
try
{
foreach (DataRow dr in Ds.Tables["datetable1"].Rows)
{
......
string insertsql = "insert into Fee
Db.UpdateData(insertsql);
LblMsg.Text = "费用数据导入成功!";
}
();
}
catch (Exception e1)
{
myTrans.Rollback();
LblMsg.Text = e1.Message + "请确认文件内容无误!";
}
finally
{
conn.Close();
myTrans.Dispose();
conn.Dispose();
}
}
以上是代码的主要部分,通过读excel中的数据逐行往数据库里插入数据,我这边想实现如果中途出现导入异常的话,能够回滚到导入前的状态,而不是仅导入部分数据。我的这个为什么不行呢,从网上找的方法!