SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text"and Name="+TextBox2.Text,sqlconn);我想验证ID和NAME都是表里的数据,用的是嵌套但语句报错,不知道怎么改,大家帮忙看看

解决方案 »

  1.   

    string sqlString = "select * from Employee where ID = '" + TextBox1.Text+"'and Name='"+TextBox2.Text+"';
    SqlCommand sqlcmd = new SqlCommand(sqlString,sqlconn);
      

  2.   

    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text              +           "and Name="+TextBox2.Text,sqlconn);
      

  3.   

    而且还少引号:
     
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID =   '    " + TextBox1.Text    +     "      '      and Name=    '      "+TextBox2.Text     +"'"       ,sqlconn);
      

  4.   

    如果你的id是数字
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text"and Name='"+TextBox2.Text+"'",sqlconn);
    如果不是数字
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = '" + TextBox1.Text"' and Name='"+TextBox2.Text+"'",sqlconn);
    但是建议你用parameter
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = @ID and Name=@name",sqlconn);
    sqlcmd.Parameters.Add("@id",txtid.Text)
    ...
      

  5.   

    1.少+号
    2."and Name"的and前面加空格
      

  6.   

    应写:
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text" and Name='"+TextBox2.Text+"'",sqlconn);
      

  7.   

    shit,编辑的时候把+号搞掉了:
    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text+" and Name='"+TextBox2.Text+"'",sqlconn);
      

  8.   

    SqlCommand sqlcmd = new SqlCommand("select * from Employee where ID = " + TextBox1.Text"and Name='"+TextBox2.Text+"'",sqlconn);