try catch 多条语句事务处理,失败回滚

  • Post author:
  • Post category:其他


List<string> sqllist = new List<string>();

string grade_sqlCmd =””;

sqllist.Add(grade_sqlCmd);

using (SqlConnection connection = new SqlConnection(SQLHelper.NukeDBConnectionString))

{

connection.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = connection;

SqlTransaction tx = connection.BeginTransaction();

cmd.Transaction = tx;

try

{

for (int n = 0; n < sqllist.Count; n++)

{

string strsql = sqllist[n];

if (strsql.Trim().Length > 1)

{

cmd.CommandText = strsql;

cmd.ExecuteNonQuery();

}

}

tx.Commit();//执行事务

return new ExecResultData(true);

}

catch

{

tx.Rollback(); //回滚事务

return new ExecResultData(“执行失败”);

}

finally

{

cmd.Dispose();

connection.Close();

}

}



版权声明:本文为oYuHuaChen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。