sqlserver ADO.net 查询数据库加锁,事务提交

//逻辑:查询表最大单号,然后锁表(TABLOKX) ,然后执行事务
using
(SqlConnection conn = EFContext.Database.Connection as SqlConnection) { System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand(); cm.Connection = conn; conn.Open(); System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction(); try { cm.Transaction = trans; //获取写进ERP的单据号 cm.CommandText = "select max(MasterId) from TBusTran WITH (TABLOCKX)"; SqlDataReader dr = cm.ExecuteReader(); while (dr.Read()) { int n = 0; while (n < dr.FieldCount) //.FieldCount获取当前行的列数 { newMasterId = GetSerialNumber_Tran(dr[n].ToString(), "P"); n++; } } dr.Close(); string sql8 = string.Format("insert into TBusQualityProblem_FlowView([FlowNo],[OperatName],[OperatDate],[OperatType],[Status])values('{0}','{1}','{2}','{3}','{4}')", MasterId, Opr_LoginId, DatetimeNow, OperatType, Status); cm.CommandText = sql8; cm.ExecuteNonQuery(); trans.Commit(); } catch (Exception) { trans.Rollback(); result.Success = false; result.Message = "操作异常!"; throw; } finally { conn.Close(); trans.Dispose(); conn.Dispose(); } }

 

转载于:https://www.cnblogs.com/zoumin123/p/9142242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值