本帖最后由 黑马-杨鹏立 于 2013-3-2 17:30 编辑
分享下对表数据删除事务回滚,在做两表删除的时候总担心删不完导致数据混乱,想到了事务回滚就把他写下来和大家一起分享。。。。希望得到大家帮助帮忙增加我的逻辑思维。。。。
#region 对表数据删除事务回滚
/// <summary>
/// 对表数据删除事务回滚
/// </summary>
/// <param name="id">待传入表1,2的通用编号</param>
/// <param name="tableName1">待传入表1</param>
/// <param name="tableName2">待传入表2</param>
/// <returns></returns>
public bool DelRollBack(int id, string tableName1, string tableName2)
{
OpenDB();
SqlTransaction sqlTran = Conn.BeginTransaction();//开始事务
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;//获取数据连接
cmd.Transaction = sqlTran;//在执行sql语句时等待提交和rollback
try
{
string sqlStr1 = "delete " + tableName1 + " where FTCPParentId=" + id + "";
cmd.CommandText = sqlStr1;
cmd.ExecuteNonQuery();
string sqlStr2 = "delete " + tableName2 + " where FCaseChildId=" + id + "";
cmd.CommandText = sqlStr2;
cmd.ExecuteNonQuery();
sqlTran.Commit();
CloseDB();
return true;
}
catch
{
sqlTran.Rollback();
CloseDB();
return false;
}
}
#endregion
|