/// <summary> /// 按合同单号执行删除 /// </summary> /// <param name="billNum"></param> /// <returns></returns> private bool DeleteBillByBillNum(string billNum) { if (!HasRight()) { throw new Exception("您没有删除记录权限!"); } //数据适配器 using (var daHead = new t_market_contract_headTableAdapter()) using (var daContent = new t_market_contract_contentTableAdapter()) using (var daAppendix = new t_market_contract_appendixTableAdapter()) { var conn = daHead.Connection; daContent.Connection = conn; daAppendix.Connection = conn; //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead)) { try { //绑定事务到适配 daHead.Transaction = tran; daContent.Transaction = tran; daAppendix.Transaction = tran; if (Convert.ToInt32(daContent.CountByBillNum(billNum)) <= 0) { throw new Exception(string.Format("当前单据:\n单号:{0}\n行号:{1}\n已经被其他用户删除!", billNum)); } //删除内容 if (daContent.DeleteByBillNum(billNum) <= 0) { throw new Exception("根据单号删除内容记录失败!"); } //删除数据表头 if (daHead.Delete(billNum) <= 0) { throw new Exception("根据单号删除表头记录失败!"); } //删除合同附件 if (daAppendix.DeleteByBillNum(billNum) <= 0) { throw new Exception("根据单号删除合同附件记录失败!"); } //提交事务 tran.Commit(); } catch (Exception ex) { //事务回滚 tran.Rollback(); throw ex; } finally { //关闭数据库连接 conn.Close(); } } } return true; }
/// <summary> /// 更新合同表头和多行内容 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e, t_market_contract_content[] arr) { //合同表头、内容、附件数据适配器 using (var daHead = new t_market_contract_headTableAdapter()) using (var daContent = new t_market_contract_contentTableAdapter()) using (var daAppendix = new t_market_contract_appendixTableAdapter()) using (var daAppendixTmp = new t_market_contract_appendix_tempTableAdapter()) { var conn = daHead.Connection; //设置为同一连接对象 daHead.Connection = daAppendix.Connection = daAppendixTmp.Connection = conn; //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead)) { try { //绑定事务 daHead.Transaction = daContent.Transaction = daAppendix.Transaction = daAppendixTmp.Transaction = tran; //原合同单号 string originalBillNum = e.Keys["bill_num"].ToString(); //新合同单号 string billNum = e.NewValues["bill_num"].ToString(); //参数 var pars = daHead.Adapter.UpdateCommand.Parameters; //新的表头数据 IOrderedDictionary values = e.NewValues; //遍历所有参数 for (int i = 0, len = pars.Count; i < len; i++) { //取字段名称 var name = pars[i].ParameterName.Replace("@", ""); //字段值 object filedValue = null; if (name == "Original_bill_num") { //原合同单号 filedValue = originalBillNum; } else { //内容 filedValue = values[name]; } var t = pars[i].DbType; //日期或时间字段为空 if ((t == DbType.DateTime || t == DbType.Date || t == DbType.DateTime2 || t == DbType.Time) && filedValue.ToString().Length == 0) { filedValue = DBNull.Value; } //记录值 pars[i].Value = filedValue; } //执行更新 int result = daHead.Adapter.UpdateCommand.ExecuteNonQuery(); if (result == 0) { //抛出错误 throw new Exception("修改合同表头失败!"); } //先删除旧合同内容 daContent.DeleteByBillNum(originalBillNum); //遍历合同内容 foreach (t_market_contract_content row in arr) { //新增一条合同内容 if (daContent.Insert(row.bill_num, row.row_id, row.material_code, row.product_num, row.size_length, row.size_width, row.pinchu, row.material_specification, row.contract_price, row.layer_count, row.process_require_chengxing, row.process_require_biaomian, row.process_require_wf, row.process_require_sm, row.unit_price_per_pcs, row.moju_rate_per_set, row.cejia_rate_per_set, row.content_remark, row.appendix_id) <= 0) { //抛出错误 throw new Exception("修改合同内容失败!"); } } //删除合同附件表 if (daAppendix.DeleteByBillNum(originalBillNum) <= 0) { //抛出错误 throw new Exception("修改合同附件失败!"); } //获取保存在临时表中的合同附件内容 var tab = daAppendixTmp.GetDataByBillNum(billNum); //遍历所有行 foreach (ydERPTY.DAL.Market.DataSetAppendix.t_market_contract_appendix_tempRow row in tab.Rows) { //新增数据到合同附件 daAppendix.Insert(row.bill_num, row.row_id, row.delivery_date, row.order_qty, row.contract_class, row.item1, row.value1, row.json1, row.item2, row.value2, row.json2, row.item3, row.value3, row.json3, row.item4, row.value4, row.json4, row.item5, row.value5, row.json5, row.item6, row.value6, row.json6, row.item7, row.value7, row.json7, row.item8, row.value8, row.json8, row.item9, row.value9, row.json9, row.item10, row.value10, row.json10, row.item11, row.value11, row.json11, row.item12, row.value12, row.json12, row.item13, row.value13, row.json13, row.item14, row.value14, row.json14, row.item15, row.value15, row.json15, row.item16, row.value16, row.json16, row.item17, row.value17, row.json17, row.item18, row.value18, row.json18, row.item19, row.value19, row.json19, row.item20, row.value20, row.json20, row.remark, row.add_person, row.add_time, row.audit, row.audit_time, row.eng, row.eng_time, row.last_change_time); } //删除合同附件临时表 daAppendixTmp.DeleteByBillNum(billNum); //提交事务 tran.Commit(); } catch (Exception ex) { //事务回滚 tran.Rollback(); throw ex; } } } return true; }