string password = txtPassWord.Text.ToString();
            string username = txtUserName.Text.ToString();
            string connStr = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=DBApp";
           // string loginSql = "select UserName from UserList where username=@UserName and password=@PassWord";
            SqlConnection conn = new SqlConnection(connStr);
            //SqlDataAdapter da = new SqlDataAdapter(loginSql, conn);
            //DataSet ds = new DataSet();
            //da.Fill(ds);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            if (txtUserName.Text.Length == 0)
            {
                MessageBox.Show("用户不能为空,请查询后输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select count(*) from UserList where UserName=='" + txtUserName.Text  + "'";
            conn.Open();
            if ((int)cmd.ExecuteScalar() == 0)
            {
                MessageBox.Show("不存在此用户,请检查后重新输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text  + "'";
            SqlDataReader myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
                MessageBox.Show("登录成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                MessageBox.Show("用户" + txtUserName .Text  + "的密码不正确", "密码", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                txtPassWord.Focus();
                return;
            }
问题1:运行后出现错误1  if ((int)cmd.ExecuteScalar() == 0) 这个有错误?
问题2: SqlDataReader myReader = cmd.ExecuteReader(); 错误??
请高手分析解答!

解决方案 »

  1.   

    这句有问题吧UserName,txtPassWord
    cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text  + "'";
      

  2.   

    这句有问题吧UserName,txtPassWord
    cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text  + "'";
    以改正不过还是出现错误: if ((int)cmd.ExecuteScalar() == 0)附近有错误!
      

  3.   

    cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text  + "'";
    楼主把
    sql=和c#==混了吧
      

  4.   

    改了之后 为什么 在用户和密码正确的情况下 f去显示("用户" + txtUserName .Text  + "的密码不正确" 比如 admin 密码123456 仍然不能登陆??