Ejemplo n.º 1
0
 /// <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);
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <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);
                    }
                }
            }
        }