//逻辑:查询表最大单号,然后锁表(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(); } }