public bool ExecuteSqlTransaction(string[] strSQL)
{
SqlCommand sc = new SqlCommand(strSQL[0], con);
sc.CommandType = CommandType.Text;
try
{
sc.Connection.Open();
}
catch
{
//MessageBox.Show("与数据库通讯异常!系统将非正常退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
using (SqlTransaction trans = con.BeginTransaction())
{
sc.Transaction = trans;
try
{
for (int i = 0; i < strSQL.Length; i++)
{
sc.CommandText = strSQL[i];
sc.ExecuteNonQuery();
}
trans.Commit();
}
catch
{
trans.Rollback();
return false;
throw;
}
finally
{
con.Close();
}
}
return true;
}
{
SqlCommand sc = new SqlCommand(strSQL[0], con);
sc.CommandType = CommandType.Text;
try
{
sc.Connection.Open();
}
catch
{
//MessageBox.Show("与数据库通讯异常!系统将非正常退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
using (SqlTransaction trans = con.BeginTransaction())
{
sc.Transaction = trans;
try
{
for (int i = 0; i < strSQL.Length; i++)
{
sc.CommandText = strSQL[i];
sc.ExecuteNonQuery();
}
trans.Commit();
}
catch
{
trans.Rollback();
return false;
throw;
}
finally
{
con.Close();
}
}
return true;
}
本文介绍了一种在数据库操作中使用SQL事务的方法,通过批量执行SQL语句并确保其原子性和一致性,从而提高数据库操作的效率和可靠性。
1555

被折叠的 条评论
为什么被折叠?



