数据库回滚操作
2012-12-06 14:29
471 查看
/// ///事务 ///
internal SqlTransaction Transaction { get; set; }
public string EditEmployee(OrgEmployee employee, Guid departmentGuid, Guid companyGuid, bool isPIC)
{
SqlPlus plus = new SqlPlus();
string result = string.Empty;
using (SqlConnection conn = plus.GetConnection())
{
conn.Open();
this.Transaction = conn.BeginTransaction();//开启事物操作
try {
this.EditEmployee(employee);
this.EditEmp2Dept(employee.EmployeeGuid, departmentGuid, companyGuid, isPIC);
this.Transaction.Commit();
}
catch (Exception ex)
{
result = ex.Message; Transaction.Rollback(); conn.Close();
}
}
return result;
}
private void EditEmp2Dept(Guid employeeGuid, Guid departmentGuid, Guid companyGuid, bool isPIC)
{
SqlParameter[] prams =
{
Database.MakeInParam("@EmployeeGuid", System.Data.SqlDbType.UniqueIdentifier, 16, employeeGuid), Database.MakeInParam("@DepartmentGuid", System.Data.SqlDbType.UniqueIdentifier, 16, departmentGuid), Database.MakeInParam("@CompanyGuid", System.Data.SqlDbType.UniqueIdentifier, 16, companyGuid),
Database.MakeInParam("@IsPIC", System.Data.SqlDbType.Bit, 1, isPIC)
};
if (Transaction == null)
{
new SqlPlus().ExecuteNonQuery(CommandType.StoredProcedure, "up_Emp2DeptEdit", prams);
}
else
{
new SqlPlus().ExecuteNonQuery(Transaction, CommandType.StoredProcedure, "up_Emp2DeptEdit", prams);
}
}
public int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行SQL命令
/// </summary>
private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandTimeout = 120;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
internal SqlTransaction Transaction { get; set; }
public string EditEmployee(OrgEmployee employee, Guid departmentGuid, Guid companyGuid, bool isPIC)
{
SqlPlus plus = new SqlPlus();
string result = string.Empty;
using (SqlConnection conn = plus.GetConnection())
{
conn.Open();
this.Transaction = conn.BeginTransaction();//开启事物操作
try {
this.EditEmployee(employee);
this.EditEmp2Dept(employee.EmployeeGuid, departmentGuid, companyGuid, isPIC);
this.Transaction.Commit();
}
catch (Exception ex)
{
result = ex.Message; Transaction.Rollback(); conn.Close();
}
}
return result;
}
private void EditEmp2Dept(Guid employeeGuid, Guid departmentGuid, Guid companyGuid, bool isPIC)
{
SqlParameter[] prams =
{
Database.MakeInParam("@EmployeeGuid", System.Data.SqlDbType.UniqueIdentifier, 16, employeeGuid), Database.MakeInParam("@DepartmentGuid", System.Data.SqlDbType.UniqueIdentifier, 16, departmentGuid), Database.MakeInParam("@CompanyGuid", System.Data.SqlDbType.UniqueIdentifier, 16, companyGuid),
Database.MakeInParam("@IsPIC", System.Data.SqlDbType.Bit, 1, isPIC)
};
if (Transaction == null)
{
new SqlPlus().ExecuteNonQuery(CommandType.StoredProcedure, "up_Emp2DeptEdit", prams);
}
else
{
new SqlPlus().ExecuteNonQuery(Transaction, CommandType.StoredProcedure, "up_Emp2DeptEdit", prams);
}
}
public int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行SQL命令
/// </summary>
private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandTimeout = 120;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
相关文章推荐
- Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
- oralce数据库的回滚操作原理
- [搬家前]数据库操作的回滚
- SSM框架——以注解形式实现事务管理,回滚数据库操作
- java 数据库操作,事务回滚
- 不同数据库间事务操作(服务器也可不同)--事务回滚
- AOP数据库操作回滚配置
- PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
- EntiryFramework中事务操作(三)事务回滚数据模型和数据库不对应问题
- 实现Guice下的单元测试中mybatis数据库操作的回滚.
- PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
- SAP EJB 的数据库回滚操作
- 数据库操作失败就【回滚】
- 伪知识之了解数据库中回滚操作的含义持续更新:【内向即失败--王奕君】
- PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
- spring框架多个数据库操作需统一提交事务回滚机制解析以及解决办法
- Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
- 数据库移植中怎样让自定义的移植操作支持回滚
- JDBC之关于数据库操作回滚研究(JDBC之四)
- Oracle数据库操作(新建数据库,导入数据,回滚)