/// <summary> /// 执行删除数据并处理相关数据一致性 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool DeleteData(ListViewDeleteEventArgs e) { //实例化数据适配器 using (var daContent = new t_sample_notice_contentTableAdapter()) //取得数据库连接 using (var conn = daContent.Connection) { //打开数据库连接 conn.Open(); //事务 using (var tran = conn.BeginTransaction()) { //设置事务 daContent.Transaction = tran; try { //当前单号 var billNum = Convert.ToString(e.Keys[0]); //当前行号 byte rowId = Convert.ToByte(e.Keys[1]); //根据单号和行号删除记录 if (daContent.Delete(billNum, rowId) <= 0) { throw new Exception("根据单号删除内容记录失败!"); } //检测当前记录内容行数 int? iCount = (int?)daContent.CountByBillNum(billNum); if (iCount.HasValue && iCount.Value <= 0) { //表头适配器 using (var daHead = new t_sample_notice_headTableAdapter()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; //删除表头 if (daHead.Delete(billNum) <= 0) { throw new Exception("根据单号删除表头记录失败!"); } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw new Exception("删除入仓单记录出现错误:\n" + ex.Message); } } } }
/// <summary> /// 根据单号从数据库中删除单据的表头和内容 /// </summary> /// <param name="billNum">单据号</param> private bool DeleteBillByBillNum(string billNum) { //实例化数据适配器 using (var daHead = new t_sample_notice_headTableAdapter()) using (var daContent = new t_sample_notice_contentTableAdapter()) //取得数据库连接 using (var conn = daHead.Connection) { //打开数据库连接 conn.Open(); //设置数据库连接 daContent.Connection = conn; //事务 using (var tran = conn.BeginTransaction()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; try { //执行删除 daHead.Delete(billNum); daContent.DeleteByBillNum(billNum); //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw new Exception("删除样板通知单记录出现错误:\n" + ex.Message); } } } }
/// <summary> /// 根据输入的参数值来执行更新数据 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e) { //数据适配器 //当前添加语句对象 //当前数据库连接 //数据适配器 using (var daHead = new t_sample_notice_headTableAdapter()) using (var daContent = new t_sample_notice_contentTableAdapter()) using (var conn = daHead.Connection) { //打开数据库连接 conn.Open(); //设置数据库连接 daContent.Connection = conn; //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; //试运行 try { //当前行单号 string billNum = Convert.ToString(e.Keys[0]); //检测是否需要保存表头 if (canEditHeadRow) { //从单据表头中删除 if (daHead.Delete(billNum) <= 0) { throw new Exception("从单据表头中删除失败!"); } //保存表头 daHead.Insert( Convert.ToDateTime(e.NewValues["bill_date"]), billNum, Convert.ToString(e.NewValues["order_id"]), Convert.ToString(e.NewValues["remark"]), Convert.ToString(e.NewValues["add_person"]), Convert.ToDateTime(e.NewValues["add_time"]), Convert.ToDateTime(e.NewValues["last_change_time"]) ); } //检测是否需要保存表身 if (canEditSonRow) { //从单据内容中删除 if (daContent.DeleteByBillNum(billNum) <= 0) { //throw new Exception("从单据内容中删除失败!"); } //获取表身内容 var rowId = Request.Form.GetValues("row_id"); var materialType = Request.Form.GetValues("material_type"); var materialCode = Request.Form.GetValues("material_code"); var materialName = Request.Form.GetValues("material_name"); var materialSize = Request.Form.GetValues("material_size"); var materialUnit = Request.Form.GetValues("material_unit"); var materialPnlLength = Request.Form.GetValues("material_pnl_length"); var materialPnlWidth = Request.Form.GetValues("material_pnl_width"); var materialPnlCountPcs = Request.Form.GetValues("material_pnl_count_pcs"); var materialPnlQty = Request.Form.GetValues("material_pnl_qty"); //取得表身内保存的行数 var iCountContentRow = 0; //遍历保存表身内容 for (int i = 0; i < rowId.Length; i++) { //有数据才保存 if ( materialType[i].Trim().Length > 0 || materialCode[i].Trim().Length > 0 || materialName[i].Trim().Length > 0 || materialUnit[i].Trim().Length > 0 || materialPnlLength[i].Trim().Length > 0 || materialPnlWidth[i].Trim().Length > 0 || materialPnlCountPcs[i].Trim().Length > 0 || materialPnlQty[i].Trim().Length > 0 ) { //执行保存 daContent.Insert( billNum, Convert.ToByte(rowId[i]), Convert.ToString(materialType[i]), Convert.ToString(materialCode[i]), Convert.ToString(materialName[i]), Convert.ToString(materialSize[i]), Convert.ToString(materialUnit[i]), Convert.ToDecimal(materialPnlLength[i]), Convert.ToDecimal(materialPnlWidth[i]), Convert.ToInt32(materialPnlCountPcs[i]), Convert.ToDecimal(materialPnlQty[i]) ); //累加已保存行数 iCountContentRow++; } } //检测行数 if (iCountContentRow <= 0) { //抛出错误 throw new Exception("单据无内容不能保存!"); } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }