在cmd.executeNonQuery
提示在此上下文不允许使用名称"xm",有效表达式包括常量,常量表达式和变量.不允许使用列名.
string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values (xm='" + value1 + "',gh='" + value2 + "',jz='" + value3 + "',lb='" + value4 + "',shijian='" + value5 + "',chuqin='" + rb + "',yy='" + ddl + "')";
  SqlCommand cmd = new SqlCommand(sql, conn);
  cmd.ExecuteNonQuery();
  conn.Close();

解决方案 »

  1.   

    你写过insert语句吗?values后面为啥要加xm='" + value1 + "',这是谁教你这么写的?
      

  2.   

    string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + rb + "','" + ddl + "')";
    我记得是这样写呢
     
      

  3.   

    "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + rb + "','" + ddl + "')";
      

  4.   

    string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values(xm='" + value1 + "',gh='" + value2 + "',jz='" + value3 + "',lb='" + value4 + "',chuqin='" + rb + "',yy='" + ddl + "',shijian='" + value5 + "')";
    顺序要对断点sql啊 拿数据库里跑哥哥啊!你这个插入插了好几天了呢。
      

  5.   

    不过你这种写法漏洞很大,安全性比较差,SQL注入就避免不了。
      

  6.   

    string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + rb + "','" + ddl + "','" + value5 + "')";
      

  7.   

    string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ("+value1+"," + value2 + "," + value3 + "," + value4 + "," + value5 + "," + rb + "," + ddl + ")
      

  8.   

    values ('" + value1 + "','" + value2 + "'……
    你前面的insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian)就是对应values里面的值不用什么在等于了,
    +是连接符
    你在查查insert语法你就明白了。
      

  9.   


    string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + rb + "','" + ddl + "','" + value5 + "')";1、注意空格
    2、注意 int 格式的值不要用''
      

  10.   

    防止SQL注入漏洞
    string sql = "insert into qqyy(@xm,@gh,@jz,@lb,@chuqin,@yy,@shijian) SqlCommand cmd = new SqlCommand(sql, conn);
     cmd.Parameters.AddWithValue("xm",xm)
    ....
    ...
    ...
      cmd.ExecuteNonQuery();
      conn.Close();
      

  11.   

    我需要获取 DropDownList 的值,这个方法是不是正确的?
    为什么插入到数据里面变成了.
    system.web.ui.webcontrols.RadioButton?
    DropDownList ddl = GridView1.Rows[i].FindControl("DropDownList1") as DropDownList;
                if (ddl.SelectedValue != "")
                {                string ddlstr = ddl.SelectedValue;
                }
      

  12.   

    你直接把控件插入进去了么
    获取值啊。不要传控件 传控件的属性参数传ddlstr 
      

  13.   

    在insert into那里,为什么没有ddlstr呢?只有ddl.
      

  14.   

    protected void Button2_Click(object sender, EventArgs e)
      {  for (int i = 0; i < this.GridView1.Rows.Count; i++)
        
      {
      string value1 = (this.GridView1.Rows[i].FindControl("Label1") as Label).Text.Trim();  
      string value2 = (this.GridView1.Rows[i].FindControl("Label2") as Label).Text.Trim();
      string value3 = (this.GridView1.Rows[i].FindControl("Label3") as Label).Text.Trim();
      string value4 = (this.GridView1.Rows[i].FindControl("Label4") as Label).Text.Trim();
      string value5 = (this.GridView1.Rows[i].FindControl("TextBox5") as TextBox).Text.Trim();
      RadioButton rb = GridView1.Rows[i].FindControl("RadioButton1") as RadioButton;
      if (rb.Checked)
      {
      string rbstr = "是";
      }
      else
      {
      string rbstr= "否";
      }
      DropDownList ddl = GridView1.Rows[i].FindControl("DropDownList1") as DropDownList;
      if (ddl.SelectedValue != "")
      {  string ddlstr = ddl.SelectedValue;
      }
      SqlConnection conn = zong.createcon();
      conn.Open();
      string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + rb + "','" + ddl + "')";
      SqlCommand cmd = new SqlCommand(sql, conn);
      cmd.ExecuteNonQuery();
      conn.Close();
        
      }
    请帮我修改一下吧,代码应该是这样的,今天写的一些在公司里面。没有拷贝回来。
      

  15.   

     protected void Button2_Click(object sender, EventArgs e)
        {        for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                string value1 = (this.GridView1.Rows[i].FindControl("Label1") as Label).Text.Trim();
                string value2 = (this.GridView1.Rows[i].FindControl("Label2") as Label).Text.Trim();
                string value3 = (this.GridView1.Rows[i].FindControl("Label3") as Label).Text.Trim();
                string value4 = (this.GridView1.Rows[i].FindControl("Label4") as Label).Text.Trim();
                string value5 = (this.GridView1.Rows[i].FindControl("TextBox5") as TextBox).Text.Trim();
                RadioButton rb = GridView1.Rows[i].FindControl("RadioButton1") as RadioButton;
                string rbstr = string.Empty;
                string ddlstr = string.Empty;
                if (rb.Checked)
                {
                    rbstr = "是";
                }
                else
                {
                    rbstr = "否";
                }
                DropDownList ddl = GridView1.Rows[i].FindControl("DropDownList1") as DropDownList;
                ddlstr = ddl.SelectedValue.ToString();
                string sql = "insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + rbstr + "','" + ddlstr + "')";
                if (ExecuteNonQuery(sql) > 0)
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('成功了');</script>");
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('悲剧了');</script>");
                }        }
        }
        public static int ExecuteNonQuery(string cmdtext)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "链接字符串";
            cn.Open();
            int value;
            try
            {
                SqlCommand cmd = new SqlCommand(cmdtext, cn);
                value = cmd.ExecuteNonQuery() > 0 ? 1 : 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return value;
        }
      

  16.   

    把下面这一段
    if (ExecuteNonQuery(sql) > 0)
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('成功了');</script>");
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('悲剧了');</script>");
                }
    去掉吧改成ExecuteNonQuery(sql);吧 在for循环里 哎
    还需要用事务暂且完成功能吧.
      

  17.   

    貌似你把insert 和 update 的语法搞混了。
      

  18.   

     string sql = " insert  into  qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + rb + "','" + ddl + "')";
      SqlCommand cmd = new SqlCommand(sql, conn);
      cmd.ExecuteNonQuery();
      conn.Close();
      

  19.   

    LZ还要检查下,要是数据库中的数据类型是数值类型的,values里面的那列就不需要加引号了,例如lb字段是数值型的,那么下面可以参考下面的string sql = " insert into qqyy(xm,gh,jz,lb,chuqin,yy,shijian) values ('" + value1 + "','" + value2 + "','" + value3 + "'," + value4 + ",'" + value5 + "','" + rb + "','" + ddl + "')";