数据未插入c#但在SQL Server查询浏览器中插入相同的数据

时间:2012-10-27 06:34:47

标签: c# asp.net sql-server

我的网格视图中有一个链接按钮,我有一些要在表中插入值,但是当我在SQL Server上测试时,它插入但不是调试模式的查询数据,因此插入了问题

protected void gvPO_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "Select")
        {
            c.GetConection();
            SqlCommand cmd = new SqlCommand("delete from tmpMateIN", c.con);
            cmd.ExecuteNonQuery();
            DataTable dt;

            int index = Convert.ToInt32(e.CommandArgument);
            gvPO.SelectedIndex = index;

            if (Convert.ToInt16(gvPO.SelectedIndex) < 0)
            {
                lblMsg.Text = "Please Select Code !";
                return;
            }

            dt = oAccount.GetPO((int)Session["CompCode"], 79, Convert.ToInt16(((LinkButton)gvPO.Rows[gvPO.SelectedIndex].Cells[0].FindControl("lnkCode")).Text.ToString()));

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                String s6 = "insert into tmpMateIN(compcode ,msttype ,mstcode,mstdate ,mstchno ,mstblno ,mstbldt ,mstcust ,itdsrno , itditem ,itdquan ,itdrema ,itemname  ,acctname ,ItmSize , unitname ,itemsize ,chno , chdt  ,godown, packsize ,itdRate , itdDisc , itdAmou  , mstInvNo , mstOrdNo,mstInvDt ,mstOrdDt ,mstrema , mstexcDes , msttaxDes ,msttaxper , mstfrghtDes , mstfrghtper , mstdeliDes ,mstpayDes , mstvaliDes, mstqno, mstqdt , itdthickness , itdlength ,itdwidth ,itdweight, itdtowt , acctaddr , custemail , mstpayMode , mstdepa,itdrefq,itdorgq)values(" + (int)Session["CompCode"] + ",79,'" + dt.Rows[i]["mstcode"] + "','" + dt.Rows[i]["mstdate"] + "'," + dt.Rows[i]["mstchno"] + ",'" + dt.Rows[i]["mstchno"] + "','" + dt.Rows[i]["mstdate"] + "','" + dt.Rows[i]["mstptcode"] + "','" + (i + 1) + "','" + dt.Rows[i]["itditem"] + "','" + dt.Rows[i]["itdquan"] + "','" + dt.Rows[i]["itdrema"] + "','" + dt.Rows[i]["itdnarr"] + "','" + dt.Rows[i]["AcctName"] + "','" + dt.Rows[i]["itdnarr"] + "','" + dt.Rows[i]["UnitName"] + "' ,'" + dt.Rows[i]["itdunit"] + "','','" + dt.Rows[i]["mstdate"] + "','',''," + dt.Rows[i]["itdRate"] + "," + dt.Rows[i]["itdamou"] + " ," + dt.Rows[i]["itdAmou"] + ",'" + dt.Rows[i]["mstInvNo"] + "','" + dt.Rows[i]["mstindno"] + "','" + dt.Rows[i]["mstdate"] + "','" + dt.Rows[i]["mstdate"] + "','" + dt.Rows[i]["mstrema"] + "','','','" + dt.Rows[i]["mstTaxPer"] + "','" + dt.Rows[i]["mstfrghtDes"] + "','" + dt.Rows[i]["mstfrghtper"] + "','" + dt.Rows[i]["mstdeliDes"] + "','" + dt.Rows[i]["mstpayDes"] + "','" + dt.Rows[i]["mstvaliDes"] + "','" + dt.Rows[i]["mstqno"] + "','" + dt.Rows[i]["mstpodate"] + "','" + dt.Rows[i]["itdthickness"] + "','" + dt.Rows[i]["itdsource"] + "','" + dt.Rows[i]["itddestin"] + "','" + dt.Rows[i]["itdweight"] + "','" + dt.Rows[i]["itdtowt"] + "','','" + dt.Rows[i]["acctaddr"] + "','" + dt.Rows[i]["mstpayMode"] + "','" + dt.Rows[i]["mstContactPerson"] + "','" + dt.Rows[i]["mstlotno"] + "','" + dt.Rows[i]["mstsection"] + "' )";

                SqlCommand cmd1 = new SqlCommand(s6, c.con);
                cmd1.ExecuteNonQuery();
            }

            c.CloseConnection();
            Response.Redirect("Poreport.aspx");
    }
}    

1 个答案:

答案 0 :(得分:1)

您可以运行SQL Server Profiler以查看是否在执行程序时调用SQL Server以进行插入。

建议:为什么不使用存储过程而不是以这种方式使用内联查询

var dr = dt.Rows[i];
SqlCommand cmd1 = new SqlCommand(s6, c.con);
cmd1.CommandType = System.Data.CommandType.StoredProcedure;
cmd1.CommandText = "tmpMateIN_Insert";
cmd1.Parameters.Add(new SqlParameter("@compcode", (int)Session["CompCode"]);
cmd1.Parameters.Add(new SqlParameter("@msttype", 79);
cmd1.Parameters.Add(new SqlParameter("@mstcode", dr["mstcode"]);
cmd1.Parameters.Add(new SqlParameter("@mstdate", dr["mstdate"]);
// ...
cmd1.ExecuteNonQuery();

<强>增加:
或者,如果你真的想坚持使用内联查询,那么首选方法是使用参数化内联查询,如下所示:

String s6 = "insert into tmpMateIN(compcode, msttype, mstcode, mstdate, ..." +
    "values(@compcode, @msttype, @mstcode, @mstdate, ...";

cmd1.Parameters.Add(new SqlParameter("@compcode", (int)Session["CompCode"]);
cmd1.Parameters.Add(new SqlParameter("@msttype", 79);
cmd1.Parameters.Add(new SqlParameter("@mstcode", dr["mstcode"]);
cmd1.Parameters.Add(new SqlParameter("@mstdate", dr["mstdate"]);
// ...
cmd1.ExecuteNonQuery();