public bool login(string name, string pwd)
        {                    
            SqlCommand comm = new SqlCommand("sellogin", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@username",name);
            comm.Parameters.AddWithValue("@password",pwd);
            conn.Open();
            int result = comm.ExecuteNonQuery(); //为什么这里获取的result的值为0,参数传进来没有错,name,pwd的值与数据库里一致
            if (result> 0)
            {   
               
                conn.Close();
                conn.Dispose();
                return true; 
                
                
            }
            else
            {
                 conn.Close();
                 conn.Dispose();
                 return false;
            }

解决方案 »

  1.   

     comm.ExecuteNonQuery();   它是返回您所执行的sql语句对数据库表所影响的行数,对于查询时,
    因为查询将不影响数据库表记录的行数,故为0.
      

  2.   


    SqlDataReader sdr=cmd.ExecuteReader();
    if (sdr.HasRows)
    {
       return true;
    }
    else
    {
      return false;
    }
      

  3.   

                Entrance log = new Entrance();//Entrance为自己所定义的一个类,创建对象将textBox1.Text, textBox2.Text的值传入            if (log.login(textBox1.Text, textBox2.Text))
                {
                    MessageBox.Show("登陆成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("登陆失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBox2.Text = "";
                }
     public   bool   login(string   name,   string   pwd) 
                    {                                         
                            SqlCommand   comm   =   new   SqlCommand( "sellogin ",   conn); 
                            comm.CommandType   =   CommandType.StoredProcedure; 
                            comm.Parameters.AddWithValue( "@username ",name); 
                            comm.Parameters.AddWithValue( "@password ",pwd); 
                            conn.Open(); 
                            SqlDataReader dr = comm.ExecuteReader(); //这里dr的值也是为NULL
                                if (dr.HasRows)                       
                             {       
                                  
                                    conn.Close(); 
                                    conn.Dispose(); 
                                    return   true;   
                                    
                                    
                            } 
                            else 
                            { 
                                      conn.Close(); 
                                      conn.Dispose(); 
                                      return   false; 
                            } 
      

  4.   

    ExecuteNonQuery()只是针对dll语句才有用,因为select时,数据库并没有发生变化,所以返回的是-1,
    int rows = (Int32)cmd.ExecuteScalar();这个可以返回影响的行数,对select同样有效
      

  5.   

    我的pwd是经过MD5加密的,存储过程没有问题,如果不进行MD5加密就可以,经过加密后,和数据库里的也一致,但就是不能成功进入
                string c = textBox2.Text;
                string md5pwd = "";
                MD5 md5 = MD5.Create();
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(c));
               for (int i = 0; i < s.Length; i++)
               {
                    md5pwd = md5pwd + s[i].ToString("X");
                }
                textBox2.Text = md5pwd;