用 StringBuilder 一次性插入多条数据到数据库

 protected void btnbc_Click(object sender, EventArgs e)
    {
        string sql2 = string.Empty;
        string sql = string.Empty;
        string dh = ddlpddh.SelectedValue.ToString();
        StringBuilder sb = new StringBuilder();//插入 开始
        StringBuilder de = new StringBuilder();//删除 开始
        sb.Append("begin "); //注意begin 后面的空格
        de.Append("begin ");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            tbyy = (System.Web.UI.MobileControls.TextBox)this.GridView1.Rows[i].FindControl("tbyy");
            if (tbyy.Text.ToString() != "")
            {
                
                string rwdh = dt.Rows[i]["rwdh"].ToString();
                string wcrq = dt.Rows[i]["wcrq"].ToString();
                sql2 = "insert into zc_pdsj_xz(rwdh,rq)values('{0}','{1}'); ";
                sql2 = string.Format(sql2, rwdh, wcrq);
                    }
                }

                
                sb.Append(sql2);
            }
            else
            {
                //页面无刷新弹框
                Page.RegisterStartupScript("", "<script>alert('请输入毁损时间!')</script>");


                return;
            }
           

        }
        try
        {
            sb.Append("end;");//插入结束
            this.dbgr.ExecSQL(sb.ToString());
            Response.Write("<script language=javascript>alert('提交成功!'); location='PKMX.aspx'</script>");


        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('出现 " + ex + " 错误'); </script>");

        }
    }
posted on 2012-07-24 09:16  小东北  阅读(1481)  评论(1编辑  收藏  举报