SqlConnection con = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=playerinfo");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from player_info where name='"+textBox1.Text+"and pwd="+textBox2.Text+ "'", con);
            int state = cmd.ExecuteNonQuery();
            con.Close();
            if (state == 0)
            {
                MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
            }
            else
            {
                Form f = new Introduce();
                this.Hide();
                f.Show();
            }
不知道为什么不行  输入错误的时候还是可以登陆呢 

解决方案 »

  1.   

    "select * from player_info where name='"+textBox1.Text+"and pwd="+textBox2.Text+ "'"
    检查下这个sql  好像有问题
      

  2.   

    "select * from player_info where name="+textBox1.Text+"and pwd="+textBox2.Text+ ""引用c#变量直接可以用双引号
      

  3.   

    如果Sql语句没问题的话,con.Close()有问题。把con.Close()写在if语句外。
      

  4.   

    if (state == 0)
                {
                    MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
                }
                else
                {
                    Form f = new Introduce();
                    this.Hide();
                    f.Show();
                }
                con.Close();
      

  5.   

    select语句不适合用ExecuteNonQuery()方法,你那个state的值永远也是"-1",你怎么操作也没用的,用其他方法吧
      

  6.   

    再补充一下 ExecuteNonQuery()方法只对 UPDATE、INSERT 和 DELETE 语句,才有返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1。
      

  7.   

    SqlConnection con = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=playerinfo");//首先检查连接字符串对不对
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from player_info where name='"+textBox1.Text+"' and pwd='"+textBox2.Text+ "'", con);//是字符串要加单引号,该有空格的用空格(and前有空格)
                int state = cmd.ExecuteNonQuery();//既然是select,不用ExecuteNonQuery,用ExecuteScalar()或者sql语句改成SELECT COUNT(*) FROM player_info  where name='"+textBox1.Text+"' and pwd='"+textBox2.Text+ "'
             //  参考http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executescalar.aspx
                con.Close();
                if (state == 0)
                {
                    MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
                }
                else
                {
                    Form f = new Introduce();
                    this.Hide();
                    f.Show();
                }
      

  8.   

    sql语句中and之前应该有个空格
      

  9.   

    楼主还是不要用ExecuteNonQuery来执行查询了吧?建议使用DataAdapter或者DataReader来实现