C#批量添加数据

来源:互联网 发布:矩阵论教材推荐 编辑:程序博客网 时间:2024/05/29 03:22


批量添加数据使用场合:ERP系统,电子商务系统,OA自动办公系统,超市管理系统;

批量添加数据前端设计原理:

批量添加数据前端设计实现:

 

完善前端数据动态添加行删除行

实现数据批量添加数据库

 

 

添加行实现步骤

  1. 引入Jquery框架文件

  2. 给指定标签注册点击事件

  3. 定义一租网页标签(要添加的行,单元格等内容)

     

    删除行数据步骤

  1. 通过$(“#name”)方法找到要删除的对象

  2. 通过找到Jequery对象,调用Remove()方法;

    如:$(“#p1”).remove();删除P1标签

     

    掌握Jquery函数:

  1. Before(function) 在每个匹配的元素的之前插入内容

  2. $(“p”).before(“<b>hello</b>:”);

  3. Click()触发每个匹配元素的Cilck();事件

  4. $(“p”).click();

     

    分析:

  1. 如何获取界面批量数据?通常在后台都是直接用组件对象的ID获取相应属性值。这里行的同吗?

  2. 获取数据如何提交给数据库相应表?

    循环提交or 一次性提交?

     

    数据库中的核心代码

    -----批量添加数据

    Insert into table

    Select ‘a’,’c’………

    Union all

    Select ‘b’,’c’………

    Union all

    Select ‘d’,’b’………

     

     

     

    首先代码实现数据的前端显示

    ---使用gridview组建,Reptater组建?

    对于批量数据的显示可以使用GridViewRepeater组件;但是为了方便后期修改和动态添加数据,需要前端手动绑定数据。基本原理<%%>,然后服务区域循环绑定。

  3. 添加数据

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>    <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>    <style type="text/css">        input{width:80px;}    </style>    <script type="text/javascript">        //预加载        $(document).ready(function () {            var i = 0;            $("#addrow").click(function () {                var tr = '<tr id=tr' + i + '><td><input name="procname" /></td><td><input name="proplus" /></td><td><input name="prowholeprice" /></td><td><input name="propprice" /></td><td><input name="prosalprice" /></td><td><input name="promakedate" /></td><td><input name="proupdate" /></td></tr>';                $("#tr").before(tr);                i++;            })            $("#delrow").click(function () {                i--;                var tr = $("#tr" + i);                tr.remove();            })        })    </script></head><body style="height: 90px">    <form id="form1" runat="server">    <div style="width:600px; margin:0px auto;">    <div style=" text-align:center; font-size:18px; font-weight:bold">产品购进系统</div>    <table border="0" cellspacing="0" cellpadding="0" width="100%">    <tr>    <td>产品名</td>            <td>产品规格</td>            <td>产品进价</td>            <td>成本价</td>            <td>销售价</td>            <td>生产日期</td>            <td>有效期</td>    </tr>        <tr>            <td><input name="procname" /></td>            <td><input name="proplus" /></td>            <td><input name="prowholeprice" /></td>            <td><input name="propprice" /></td>            <td><input name="prosalprice" /></td>            <td><input name="promakedate" /></td>            <td><input name="proupdate" /></td>        </tr>        <tr id="tr">            <td colspan="7" align="right">                 <input type="button" id="addrow" value="添加行"/>                 <input type="button" id="delrow" value="删除行"/>            </td>        </tr>        <tr id="tr1">            <td colspan="7" align="right">                <asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click" />                <asp:Button ID="BtnAdd2" runat="server" Text="添加2" onclick="BtnAdd2_Click" />            </td>        </tr>    </table>    </div>    </form></body></html>



 protected void btnAdd_Click(object sender, EventArgs e)        {            string procnames = Request.Form["procname"].ToString();            string proplus = Request.Form["proplus"].ToString();            string prowholeprices = Request.Form["prowholeprice"].ToString();            string propprices = Request.Form["propprice"].ToString();            string prosalprices = Request.Form["prosalprice"].ToString();            string promakedates = Request.Form["promakedate"].ToString();            string proupdates = Request.Form["proupdate"].ToString();            string[] procnamelist = procnames.Split(',');            string[] propluslist = proplus.Split(',');            string[] prowholepriceslist = prowholeprices.Split(',');            string[] proppriceslist = propprices.Split(',');            string[] prosalpriceslist = prosalprices.Split(',');            string[] promakedateslist = promakedates.Split(',');            string[] proupdateslist = proupdates.Split(',');                        for (int i=0;i<procnamelist.Length;i++)            {                        string strSql = "insert into tbProduct values(@procname,@proplu,@prowholeprice,@propprice,@prosalprice,@promakedate,@proupdate)";            SqlParameter[] parms = new SqlParameter[]{                new SqlParameter("@procname",procnamelist[i]),                new SqlParameter("@proplu",propluslist[i]),                new SqlParameter("@prowholeprice",prowholepriceslist[i]),                new SqlParameter("@propprice",proppriceslist[i]),                new SqlParameter("@prosalprice",prosalpriceslist[i]),                new SqlParameter("@promakedate",promakedateslist[i]),                                new SqlParameter("@proupdate",proupdateslist[i]),            };            string conn = "user id=hdp;password=hdp;initial catalog=Demo1;data source=JUPSXQNQB0MIHD4\\SQLEXPRESS";            int result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql, parms);            }        }        protected void BtnAdd2_Click(object sender, EventArgs e)        {            string procnames = Request.Form["procname"].ToString();            string proplus = Request.Form["proplus"].ToString();            string prowholeprices = Request.Form["prowholeprice"].ToString();            string propprices = Request.Form["propprice"].ToString();            string prosalprices = Request.Form["prosalprice"].ToString();            string promakedates = Request.Form["promakedate"].ToString();            string proupdates = Request.Form["proupdate"].ToString();            string[] procnamelist = procnames.Split(',');            string[] propluslist = proplus.Split(',');            string[] prowholepriceslist = prowholeprices.Split(',');            string[] proppriceslist = propprices.Split(',');            string[] prosalpriceslist = prosalprices.Split(',');            string[] promakedateslist = promakedates.Split(',');            string[] proupdateslist = proupdates.Split(',');            string strSql = "insert into tbProduct ";            for (int i = 0; i < procnamelist.Length;i++ )            {                strSql += "select '" + procnamelist[i] + "','" + propluslist[i] + "'," + decimal.Parse(prowholepriceslist[i]) + "," + decimal.Parse(proppriceslist[i]) + "," + decimal.Parse(prosalpriceslist[i]) + ",'" + DateTime.Parse(promakedateslist[i]) + "','" + DateTime.Parse(proupdateslist[i]) + "' union all ";                string newSql=strSql.Substring(0, strSql.LastIndexOf("union all"));                string conn = "user id=hdp;password=hdp;initial catalog=Demo1;data source=JUPSXQNQB0MIHD4\\SQLEXPRESS";                int result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, newSql, null);            }        }    }
4.数据显示
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script language="javascript" type="text/javascript">    </script></head><body>    <form id="form1" runat="server">    <div style="width:600px; margin:0px auto;">    <div style=" text-align:center; font-size:18px; font-weight:bold">产品购进系统</div>    <table border="0" cellspacing="0" cellpadding="0" width="100%">    <tr>    <td>产品名</td>            <td>产品规格</td>            <td>产品进价</td>            <td>成本价</td>            <td>销售价</td>            <td>生产日期</td>            <td>有效期</td>    </tr>        <%            if (dt != null && dt.Rows.Count > 0)            {                for (int i = 0; i < dt.Rows.Count; i++)                {%>                <tr>                    <td><input name="procname" / value="<%=dt.Rows[i]["proname"]%>"></td>                    <td><input name="proplus" value="<%=dt.Rows[i]["proplus"]%>"/></td>                    <td><input name="prowholeprice" value="<%=dt.Rows[i]["prowholprice"]%>"/></td>                    <td><input name="propprice" value="<%=dt.Rows[i]["proprice"]%>"/></td>                    <td><input name="prosalprice" value="<%=dt.Rows[i]["prosalprice"]%>"/></td>                    <td><input name="promakedate" value="<%=dt.Rows[i]["promakedate"]%>"/></td>                    <td><input name="proupdate" value="<%=dt.Rows[i]["proexpdate"]%>"/></td>                 </tr>              <%}            }                                   %>                <tr id="tr">            <td colspan="7" align="right">                 <input type="button" id="addrow" value="添加行" " />                 <input type="button" id="delrow" value="删除行"/>            </td>        </tr>        <tr id="tr1">            <td colspan="7" align="right">                <asp:Button ID="btnAdd" runat="server" Text="添加"  />                <asp:Button ID="BtnAdd2" runat="server" Text="添加2"  />            </td>        </tr>    </table>    </div>    </form></body></html>

public partial class List : System.Web.UI.Page    {        public DataTable dt;        protected void Page_Load(object sender, EventArgs e)        {            string strSql="SELECT Id,proname,proplus,prowholprice,proprice,prosalprice,promakedate,proexpdate FROM tbProduct";            string conn = "user id=hdp;password=hdp;initial catalog=Demo1;data source=JUPSXQNQB0MIHD4\\SQLEXPRESS";            dt = SqlHelper.ExecuteDataSet(conn, CommandType.Text, strSql, null).Tables[0];        }    }



0 0