您的位置:首页 > 其它

ADO.NET事务的实现

2014-12-03 14:10 211 查看
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
public bool Delete(int id)
{
bool flag=false;
     using (SqlConnection conn = new SqlConnection("连接字符串"))
{
conn.Open();//<span style="color:#FF6666;">打开数据库(重要)</span>
SqlTransaction tran = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();

StringBuilder sbSql = new StringBuilder();
sbSql.Append("    delete from PM_Product_Property where PSUKID=" + skuID);
sbSql.Append("    delete from  PM_ProductSKU where ID=" + skuID);
<pre name="code" class="csharp">                cmd.Connection = conn;
cmd.Transaction = tran;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sbSql.ToString();

int i = cmd.ExecuteNonQuery();
if (i > 0)
{
flag = true;
tran.Commit();//<span style="color:#FF6666;">成功提交</span>
}
else
{
flag = false;
tran.Rollback();//<span style="color:#FF6666;">失败回滚</span>
}
return flag;
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: