Example #1
0
        /// <summary>
        /// 根据输入的参数值来执行更新数据
        /// </summary>
        /// <param name="e">传入的带有数据的事件参数</param>
        /// <returns></returns>
        private bool UpdateData(FormViewUpdateEventArgs e)
        {
            //数据适配器
            //当前添加语句对象
            //当前数据库连接
            using (var da = new v_material_backTableAdapter())
            using (var conn = da.Connection)
            {
                //打开数据库连接
                conn.Open();
                //开启事务
                using (var tran = conn.BeginTransaction())
                {
                    //设置事务
                    da.Transaction = tran;
                    //试运行
                    try
                    {
                        //当前行单号
                        string billNum = Convert.ToString(e.Keys[0]);
                        //获取数据
                        using (var tab = da.GetDataByBillNum(billNum))
                        {
                            //检查是否获取到行
                            if (tab.Rows.Count == 0)
                            {
                                //显示失败
                                throw new Exception("当前记录已经被其他用户删除!");
                            }
                            //数据适配器
                            using (var daHead = new t_material_back_headTableAdapter())
                            using (var daContent = new t_material_back_contentTableAdapter())
                            using (var daBalance = new t_material_balanceTableAdapter())
                            {
                                //设置数据库连接
                                daHead.Connection = daContent.Connection = daBalance.Connection = conn;
                                //设置事务
                                daHead.Transaction = daContent.Transaction = daBalance.Transaction = tran;
                                //将之前的单据内容写回
                                foreach (DataSetMaterialBack.v_material_backRow row in tab.Rows)
                                {
                                    //从原料结存清单中扣除
                                    if (!ydOperateMaterial.DecreaseMaterialBalance(
                                        daBalance,
                                        BillType.Back,
                                        row.supplier_name,
                                        row.material_name,
                                        row.price,
                                        row.qty
                                    ))
                                    {
                                        return false;
                                    }
                                }
                                //从单据内容中删除
                                if (daContent.DeleteByBillNum(billNum) <= 0)
                                {
                                    throw new Exception("从单据内容中删除失败!");
                                }
                                //从单据表头中删除
                                if (daHead.Delete(billNum) <= 0)
                                {
                                    throw new Exception("从单据表头中删除失败!");
                                }

                                //日期
                                DateTime billDate = Convert.ToDateTime(e.NewValues["bill_date"]);
                                //单据备注
                                string billRemark = Convert.ToString(e.NewValues["remark"]);
                                //录入员
                                string addPerson = e.NewValues["add_person"].ToString();
                                //录入时间
                                DateTime addTime = Convert.ToDateTime(e.NewValues["add_time"]);
                                //最后修改时间
                                DateTime lastChangeTime = Convert.ToDateTime(e.NewValues["last_change_time"]);
                                //含有数据的行数
                                int iCountContent = 0;
                                //遍历子表执行保存
                                for (int iRow = 0; iRow < 10; iRow++)
                                {
                                    //子表各控件
                                    var tr = tabDataListSon.Rows[iRow + 1];
                                    var litRowId = (Literal)tr.Cells[0].Controls[0];
                                    var txtSupplierCode = (TextBox)tr.Cells[1].Controls[0];
                                    var txtSupplierName = (TextBox)tr.Cells[2].Controls[0];
                                    var txtMaterialCode = (TextBox)tr.Cells[3].Controls[0];
                                    var txtMaterialName = (TextBox)tr.Cells[4].Controls[0];
                                    var txtMaterialSize = (TextBox)tr.Cells[5].Controls[0];
                                    var txtQty = (TextBox)tr.Cells[6].Controls[0];
                                    var txtMaterialUnit = (TextBox)tr.Cells[7].Controls[0];
                                    var txtPrice = (TextBox)tr.Cells[8].Controls[0];
                                    var txtRemark = (TextBox)tr.Cells[9].Controls[0];
                                    //取得数据
                                    byte rowId = Convert.ToByte(litRowId.Text);
                                    string supplierCode = txtSupplierCode.Text;
                                    string supplierName = txtSupplierName.Text;
                                    string materialCode = txtMaterialCode.Text;
                                    string materialName = txtMaterialName.Text;
                                    string materialSize = txtMaterialSize.Text;
                                    decimal qty = txtQty.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtQty.Text.Trim());
                                    string materialUnit = txtMaterialUnit.Text;
                                    decimal price = txtPrice.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtPrice.Text.Trim());
                                    string remark = txtRemark.Text;
                                    //存在数据才保存
                                    if (materialCode.Length > 0 && materialName.Length > 0
                                        && materialUnit.Length > 0 && qty > 0)
                                    {
                                        //保存到原料结存清单
                                        if (daBalance.InsertData(
                                            supplierCode,
                                            supplierName,
                                            materialCode,
                                            materialName,
                                            materialSize,
                                            qty,
                                            materialUnit,
                                            price,
                                            remark,
                                            addPerson,
                                            false
                                        ) <= 0)
                                        {
                                            throw new Exception("保存到原料结存清单失败!");
                                        }
                                        //保存到单据内容清单
                                        if (daContent.Insert(
                                            billNum,
                                            rowId,
                                            supplierCode,
                                            supplierName,
                                            materialCode,
                                            materialName,
                                            materialSize,
                                            qty,
                                            materialUnit,
                                            price,
                                            remark
                                        ) <= 0)
                                        {
                                            throw new Exception("保存到单据内容清单失败!");
                                        }
                                        //含有数据的行数计数
                                        iCountContent++;
                                    }
                                }

                                //保存表头
                                if (iCountContent > 0)
                                {
                                    if (daHead.Insert(
                                        billDate,
                                        billNum,
                                        billRemark,
                                        addPerson,
                                        addTime,
                                        lastChangeTime
                                    ) <= 0)
                                    {
                                        throw new Exception("保存到单据表头清单失败!");
                                    }
                                }
                            }
                        }
                        //提交事务
                        tran.Commit();
                        //返回成功
                        return true;
                    }
                    catch (Exception ex)
                    {
                        //回滚事务
                        tran.Rollback();
                        //非数字返回失败
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// 根据输入的LOT格式来保存多条数据到数据库
        /// </summary>
        /// <param name="e">传入的带有数据的事件参数</param>
        /// <returns></returns>
        private bool InsertData(FormViewInsertEventArgs e)
        {
            //日期
            DateTime billDate = Convert.ToDateTime(e.Values["bill_date"]);
            //单号
            string billNum = Convert.ToString(e.Values["bill_num"]);
            //单据备注
            string billRemark = Convert.ToString(e.Values["remark"]);
            //录入员
            string addPerson = e.Values["add_person"].ToString();
            //录入时间
            DateTime addTime = Convert.ToDateTime(e.Values["add_time"]);
            //最后修改时间
            DateTime lastChangeTime = Convert.ToDateTime(e.Values["last_change_time"]);
            //数据适配器
            //当前添加语句对象
            //当前数据库连接
            using (var daHead = new t_material_back_headTableAdapter())
            using (var daContent = new t_material_back_contentTableAdapter())
            using (var daBalance = new t_material_balanceTableAdapter())
            using (var conn = daHead.Connection)
            {
                //打开数据库连接
                conn.Open();
                //开启事务
                using (var tran = conn.BeginTransaction())
                {
                    //设置数据库连接对象
                    daContent.Connection = daBalance.Connection = conn;
                    //设置事务
                    daHead.Transaction = daContent.Transaction = daBalance.Transaction = tran;
                    //试运行
                    try
                    {
                        //含有数据的行数
                        int iCountContent = 0;
                        //遍历子表执行保存
                        for (int iRow = 0; iRow < 10; iRow++)
                        {
                            //子表各控件
                            var tr = tabDataListSon.Rows[iRow + 1];
                            var litRowId = (Literal)tr.Cells[0].Controls[0];
                            var txtSupplierCode = (TextBox)tr.Cells[1].Controls[0];
                            var txtSupplierName = (TextBox)tr.Cells[2].Controls[0];
                            var txtMaterialCode = (TextBox)tr.Cells[3].Controls[0];
                            var txtMaterialName = (TextBox)tr.Cells[4].Controls[0];
                            var txtMaterialSize = (TextBox)tr.Cells[5].Controls[0];
                            var txtQty = (TextBox)tr.Cells[6].Controls[0];
                            var txtMaterialUnit = (TextBox)tr.Cells[7].Controls[0];
                            var txtPrice = (TextBox)tr.Cells[8].Controls[0];
                            var txtRemark = (TextBox)tr.Cells[9].Controls[0];
                            //取得数据
                            byte rowId = Convert.ToByte(litRowId.Text);
                            string supplierCode = txtSupplierCode.Text;
                            string supplierName = txtSupplierName.Text;
                            string materialCode = txtMaterialCode.Text;
                            string materialName = txtMaterialName.Text;
                            string materialSize = txtMaterialSize.Text;
                            decimal qty = txtQty.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtQty.Text.Trim());
                            string materialUnit = txtMaterialUnit.Text;
                            decimal price = txtPrice.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtPrice.Text.Trim());
                            string remark = txtRemark.Text;
                            //存在数据才保存
                            if (materialCode.Length > 0 && materialName.Length > 0 && materialUnit.Length > 0 && qty > 0)
                            {
                                //保存到原料结存清单
                                if (daBalance.InsertData(
                                    supplierCode,
                                    supplierName,
                                    materialCode,
                                    materialName,
                                    materialSize,
                                    qty,
                                    materialUnit,
                                    price,
                                    remark,
                                    addPerson,
                                    false
                                ) <= 0)
                                {
                                    throw new Exception("保存到原料结存清单失败!");
                                }
                                //保存到单据内容清单
                                if (daContent.Insert(
                                    billNum,
                                    rowId,
                                    supplierCode,
                                    supplierName,
                                    materialCode,
                                    materialName,
                                    materialSize,
                                    qty,
                                    materialUnit,
                                    price,
                                    remark
                                ) <= 0)
                                {
                                    throw new Exception("保存到单据内容清单失败!");
                                }
                                //含有数据的行数计数
                                iCountContent++;
                            }
                        }

                        //保存表头
                        if (iCountContent > 0)
                        {
                            if (daHead.Insert(
                                 billDate,
                                 billNum,
                                 billRemark,
                                 addPerson,
                                 addTime,
                                 lastChangeTime
                             ) <= 0)
                            {
                                throw new Exception("保存到单据表头清单失败!");
                            }
                        }
                        //提交事务
                        tran.Commit();
                        //返回成功
                        return true;
                    }
                    catch (Exception ex)
                    {
                        //回滚事务
                        tran.Rollback();
                        //非数字返回失败
                        throw new Exception(ex.Message);
                    }
                }
            }
        }