Enterprise Library事务不能回滚
用企业库写了个事务,但是回滚不了。
protected void Button3_Click(object sender, EventArgs e)
{
bool bResult = true;
Database db = DatabaseFactory.CreateDatabase();
string strSQL = "update menugroup set namef='第一个内容' where id=1"; // 将name写成namef,形成一个错误
DbCommand updateCmd = db.GetSqlStringCommand(strSQL);
strSQL = "update menugroup set name='第二内容' where id=2";
DbCommand delCmd = db.GetSqlStringCommand(strSQL);
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
{
db.ExecuteNonQuery(delCmd);
db.ExecuteNonQuery(updateCmd);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
bResult = false;
}
connection.Close();
}
if (bResult)
this.Label1.Text = "事务成功";
else
this.Label1.Text = "事务失败";
}
程序执行后id=1的记录没有改变,但id=2的记录确被修改了,怎么没有回滚?Label1显示"事务失败"