问题描述
- winform 项目删除sql server事务问题
-
当我点击删除时报下面错误,是什么意思? 怎么解决?项目是winform 数据库是sql server如果分配给命令的连接位于本地挂起事务中,ExecuteReader 要求命令拥有事务。命令的 Transaction 属性尚未初始化。
事务部分代码
bool isSucess = false;
using (DbBase DbContext = new DbBase(connectionName))
{
using (IDbTransaction tran = DbContext.DbTransaction)
{
try
{
var query = new DapperExQuery().AndWhere(item => item.S_CODE,
OperationMethod.Equal, param.S_CODE);//根据店铺code查询所有采购回复信息
var replyQuery = query.GetSqlQuery(DbContext);
T_PRODUCT_REPLY entity = DbContext.SingleOrDefault(replyQuery, tran);
if (entity != null)
{
isSucess = DbContext.Delete(replyQuery, tran);
if (isSucess)
{
var purchaseQuery =
new DapperExQuery().AndWhere(item => item.PP_CODE,
OperationMethod.Equal, entity.PP_CODE);//根据采购PP_CODE查询采购信息
var purchaseSqlQuery = purchaseQuery.GetSqlQuery(DbContext);T_PRODUCT_PURCHASE pEntity = DbContext.SingleOrDefault<T_PRODUCT_PURCHASE>(purchaseSqlQuery, tran); if (pEntity != null) { isSucess = DbContext.Delete<T_PRODUCT_PURCHASE>(purchaseSqlQuery, tran); } } } else { isSucess = true; } if (isSucess) { } } catch (Exception e) { isSucess = false; throw e; } finally { if (isSucess) { tran.Commit(); } else { tran.Rollback(); } } } return isSucess; }
解决方案
SQL Server事务
Sql server 事务的两种用法
SQL Server 事务日志的问题