• ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)


    这是第四步点击保存将信息存入数据库中。

    这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中。代码如下:

    一、界面获取数据JS代码:

    //保存订单信息
    function SaveToJson() {
        //单位
        var Company = document.getElementById("lblCompany").innerHTML;
        //NO
        var OrderNo = document.getElementById("lblNO").innerHTML;
        //下单类型
        var XDLX = document.getElementById("ddXDLX").value;
        //转库凭证号
        var ZKPZH = document.getElementById("txtZKPZH").value;
        //日期
        var OrderDate = document.getElementById("lblDate").innerHTML;
        //代理商ID
        var intDLSID = document.getElementById("hidDLSID").value;
        strReturned = returned.toString()
    
        $.post("../DDGL/handler/SaveZCXDInfo.ashx", { strReturned: strReturned, Company: Company, OrderNo: OrderNo, XDLX: XDLX, ZKPZH: ZKPZH, OrderDate: OrderDate, intDLSID: intDLSID }, function(data) {
            if (data == "True") {
                alert("添加成功!")
            } else {
                alert("添加失败!")
            }
            window.location.href = "QueryDJLJList.aspx";
        });
    
    }

    二、SaveZCXDInfo.ashx代码。

    这个代码中我将JSON字符串转化成对象,在将对象转化成和数据库表结构一致的DataTable。代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;
    using Model;
    using BLL;
    using System.Data;
    
    namespace WEB.DDGL.handler
    {
    
        public class QueryTotalMoney : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //获取主表信息            
                int intDLSID = Convert.ToInt32(context.Request.Form["intDLSID"].ToString().Trim());
                string strLJInfo = context.Request.Form["strReturned"].ToString();
                string strCompany = context.Request.Form["Company"].ToString();
                string strOrderNo = context.Request.Form["OrderNo"].ToString();
                string strXDLX = context.Request.Form["XDLX"].ToString();
                string strZKPZH = context.Request.Form["ZKPZH"].ToString();
                string strOrderDate = context.Request.Form["OrderDate"].ToString();
                //主表插入信息
                MT_DLS_Order Order = new MT_DLS_Order();
                Order.beginDate = strOrderDate;
                Order.Company = strCompany;
                Order.DLSID = intDLSID.ToString();
                Order.XDLX = strXDLX;
                Order.ZKPZH = strZKPZH;
                Order.OrderCode = strOrderNo;
                Order.SFQBFH = "否";
                Order.IsCancel = "否";
                Order.SFJRFee = "否";
                //获取从表信息
                /*
                 *  1、新建一个DataTable
                 *  2、把传来的Json字符串改成数组
                 *  3、把数组变成对象
                 *  4、把对象的值赋给DataTable
                 * */
                //新建一个DataTable
                DataTable dtLJInfo = new DataTable("T_DLS_Order_Cong");
                dtLJInfo.TableName = "T_DLS_Order_Cong";
                //添加DataTable数据项
                dtLJInfo.Columns.Add(new DataColumn("ID", typeof(int)));
                dtLJInfo.Columns.Add(new DataColumn("name", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("beginDate", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("OrderID", typeof(int)));
                dtLJInfo.Columns.Add(new DataColumn("LJID", typeof(int)));
                dtLJInfo.Columns.Add(new DataColumn("LJBM", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("LJMC", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("Numbers", typeof(int)));
                dtLJInfo.Columns.Add(new DataColumn("Money", typeof(decimal)));
                dtLJInfo.Columns.Add(new DataColumn("DJNo", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("SFFH", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("FHRNo", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("FHRName", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("FHDate", typeof(string)));
                dtLJInfo.Columns.Add(new DataColumn("IsCancel", typeof(string)));
                //把Json字符串改成数组
                JArray jaLJInfo = (JArray)JsonConvert.DeserializeObject(strLJInfo);
                if (strXDLX == "正常下单")
                {
                    for (int i = 0; i < jaLJInfo.Count; i++)
                    {
                        JObject obj = (JObject)jaLJInfo[i];
                        DataRow drLJInfo = dtLJInfo.NewRow();
                        string strLJID = obj["LJID"].ToString().Trim();
                        string strNumbers = obj["SL"].ToString();
                        string strMoney = obj["Money"].ToString();
                        string strLJBM = obj["LJBM"].ToString();
                        string strLJMC = obj["LJMC"].ToString();
                        strLJID = strLJID.Replace(""", "");
                        strNumbers = strNumbers.Replace(""", "");
                        strMoney = strMoney.Replace(""", "");
                        strLJBM = strLJBM.Replace(""", "");
                        strLJMC = strLJMC.Replace(""", "");
                        drLJInfo["LJID"] = Convert.ToInt32(strLJID);
                        drLJInfo["LJBM"] = strLJBM;
                        drLJInfo["LJMC"] = strLJMC;
                        drLJInfo["Numbers"] = Convert.ToInt32(strNumbers);
                        drLJInfo["Money"] = Convert.ToDecimal(strMoney);
                        drLJInfo["beginDate"] = strOrderDate;
                        drLJInfo["IsCancel"] = "否";
                        drLJInfo["SFFH"] = "否";
                        dtLJInfo.Rows.Add(drLJInfo);
                    }
                }
                else
                {
                    for (int i = 0; i < jaLJInfo.Count; i++)
                    {
                        JObject obj = (JObject)jaLJInfo[i];
                        DataRow drLJInfo = dtLJInfo.NewRow();
                        string strLJID = obj["LJID"].ToString().Trim();
                        string strMoney = obj["Money"].ToString();
                        string strLJBM = obj["LJBM"].ToString();
                        string strLJMC = obj["LJMC"].ToString();
                        string strDJNo = obj["DJBH"].ToString();
    
                        strLJID = strLJID.Replace(""", "");
                        strMoney = strMoney.Replace(""", "");
                        strLJBM = strLJBM.Replace(""", "");
                        strLJMC = strLJMC.Replace(""", "");
                        strDJNo = strDJNo.Replace(""", "");
                        drLJInfo["LJID"] = Convert.ToInt32(strLJID);
                        drLJInfo["LJBM"] = strLJBM;
                        drLJInfo["LJMC"] = strLJMC;
                        drLJInfo["Money"] = Convert.ToDecimal(strMoney);
                        drLJInfo["beginDate"] = strOrderDate;
                        drLJInfo["DJNo"] = strDJNo;
                        drLJInfo["SFFH"] = "否";
                        drLJInfo["IsCancel"] = "否";
                        dtLJInfo.Rows.Add(drLJInfo);
                    }
                }
    
                BT_XDMgr XDMgr = new BT_XDMgr();
                if (XDMgr.AddOrder(Order, dtLJInfo))
                {
                    context.Response.Write("True");
    
                }
                else
                {
                    context.Response.Write("False");
                }
    
                context.Response.End();
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    

    三、BLL层代码。

        /// <summary>
        /// 添加到数据库
        /// </summary>
        /// <param name="Order"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
            public bool AddOrder(MT_DLS_Order Order,DataTable dt)
            {
                
                DT_DLS_Order OrderDal = new DT_DLS_Order();
                DT_DLS_Order_Cong OrderCongDal = new DT_DLS_Order_Cong();
                DT_DLS_Order_LC OrderLCDal = new DT_DLS_Order_LC();
                decimal TotalMoney=0;
                //添加主单信息
                int OrderID = OrderDal.Add(Order);
                //添加订单ID在从表中
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["OrderID"] = OrderID;
                    TotalMoney = TotalMoney +Convert.ToDecimal( dt.Rows[i]["Money"].ToString().Trim()); 
                }
                //更新总金额
                Order.ID = OrderID;
                Order.OrderMoney = TotalMoney;
                //判断是否更新成功
                if (OrderDal.Update(Order))
                {
                    //添加订单从表
                    //判断是否添加从表成功
                    if (OrderCongDal.AddOrderInfoByDataTable(dt))
                    {
                        //添加此订单的流程
                        //查询出流程ID
                        //查询所有的节点ID
                        int ZFGDLS = Convert.ToInt32(ConfigurationSettings.AppSettings["ZFGDLS"].ToString());
                        DataTable dtZFGDLSJD = new DataTable();
                        dtZFGDLSJD = OrderLCDal.QueryAllJD(ZFGDLS);
                        //判断流程中是否有节点
                        if (dtZFGDLSJD.Rows.Count > 0)
                        {
                            DataTable dtOrderLC = new DataTable();
                            dtOrderLC.TableName = "T_DLS_Order_LC";
                            dtOrderLC.Columns.Add(new DataColumn("ID", typeof(int)));
                            dtOrderLC.Columns.Add(new DataColumn("name", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("beginDate", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("OrderID", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("LCID", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("State", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("CZRNo", typeof(string)));
                            dtOrderLC.Columns.Add(new DataColumn("CZRName", typeof(string)));
                           
                            for (int i = 0; i < dtZFGDLSJD.Rows.Count; i++)
                            {
                                DataRow drOrderLCInfo = dtOrderLC.NewRow();
                               
                                drOrderLCInfo["OrderID"] = Order.ID;
                                drOrderLCInfo["LCID"] = dtZFGDLSJD.Rows[i]["ID"].ToString().Trim();
    
                                if (i == 0)
                                {
                                    drOrderLCInfo["State"] = "是";
                                    drOrderLCInfo["CZRNo"] = Order.DLSID;
                                    drOrderLCInfo["CZRName"] = Order.Company;
                                    drOrderLCInfo["beginDate"] = Order.beginDate;
                                }
                                else
                                {
                                    drOrderLCInfo["State"] = "否";
                                }
                                dtOrderLC.Rows.Add(drOrderLCInfo);
                            }
                           //判断添加流程是否成功
                            if (OrderLCDal.AddOrderLC(dtOrderLC))
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                           
                        }
                        else
                        {
                            return false;
                        }
                    
                    }
                    else
                    {
                        return false;
                    }
                   
                }
                else
                {
                    return false;
                }
               
               
            }

    四、D层中关键的代码。

            将传回去的DataTable直接存到数据库中:

             代码如下:

    /// <summary>
        /// 将datatable中的数据批量插入数据库
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static Boolean makeDatatableIntoDatabase(DataTable dt)
        {
            SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
            sqlbulkcopy.DestinationTableName = dt.TableName;//数据库中的表名
    
            try
            {
                sqlbulkcopy.WriteToServer(dt);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

           这里值得注意的是:我们传过来的DataTable TableName要和数据库中表名一致,DataTable中的字段顺序,类型、名称要和数据库中的字段字段顺序,类型、名称一致才行。

    五、效果图。


    六、综述。

            就这四步把整个功能一一展现出来了用到的知识点很多。从这个功能中我的体会就是实习完回去赶紧多做些这样的小Demo保存好工作的时候直接用不要等到工作了慢慢的研究。其实我们做项目都是一个个小Demo拼接出来的。所以多做Demo工作上遇到了开发效率就高了。

    
    

    Meet so Meet. C plusplus I-PLUS....
  • 相关阅读:
    mysql语句
    jsp中调用javabean
    java内存分配
    BaseClasses学习(-)CAMEvent
    YUV格式入门
    gcc intrinsic vector
    GCC builtin vector (gcc内建函数)学习
    GNU 内联汇编学习(1)
    express 中间件
    《饿了么大前端 Node.js 进阶教程》—Javascript 基础问题—引用传递
  • 原文地址:https://www.cnblogs.com/iplus/p/4490378.html
Copyright © 2020-2023  润新知