C# 事务之SqlTransaction
时间:2021-07-01 10:21:17
帮助过:3人阅读
static void Execute(
string connectionString)
{
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
SqlTransaction transaction;
using (SqlCommand cmd =
connection.CreateCommand())
{
//启动事务
transaction =
connection.BeginTransaction();
cmd.Connection =
connection;
cmd.Transaction =
transaction;
try
{
cmd.CommandText =
"sql语句!";
cmd.ExecuteNonQuery();
//完成提交
transaction.Commit();
}
catch(Exception ex)
{
//数据回滚
transaction.Rollback();
throw ex;
}
}
}
}
在ADO.NET中使用SqlTransaction事务的例子代码,需要说明的已经写成注释。参考:http://www.cnblogs.com/wang726zq/archive/2012/05/17/Transaction.html
C# 事务之SqlTransaction
标签: