public bool yanzheng(string id, string pwd, string logintype)
        {
            bool b = false;
            conn();//打开数据连接
            if(logintype=="管理员")
            {
                string sql1 = string.Format("select count(*) from admin wherer loginid='{0}'", id);
                cmd = new SqlCommand(sql1, con);
                int num = 0;
                num = (int)cmd.ExecuteScalar();//用户名数量
                if (num == 1)
                {
                   string sql = string.Format("select '{0}'from admin where loginid='{1}'", pwd, id);
                    SqlCommand cmd1 = new SqlCommand(sql, con);
                    read = cmd1.ExecuteReader();
                    while (read.Read())
                    {
                        string s = (string)read["loginpwd"];
                        if (s == pwd)
                        {
                            b= true;
                            return b;//1号位置
                        }
                        else
                        { 
                MessageBox.Show("密码错误", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                               b=false;
                               return b;//2号位置
                        }
                    }
                    return b;//3号位置
                }
                else
                {
                   
          MessageBox.Show("用户名不存在", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    b= false;
                    return b;//4号位置
                }
                return b;//5号位置
            }
            //下面还有一样的结构判断别的登录类型
如t 不管我在那个位置加return的值 都显示 
错误
“MySchool.Db.yanzheng(string, string, string)”: 并非所有的代码路径都返回值

解决方案 »

  1.   

    public bool yanzheng(string id, string pwd, string logintype) 
            { 
                bool b = false; 
    只有方法有返回值
    一定要在最后return;
    你看看你是不是最后一行没有这句话
    retrun b;
    }
      

  2.   

     if(logintype!="管理员") 则没有返回值
      

  3.   

    logintype!="管理员" 时没返回
      

  4.   

    你总的if判断返回b
    那么else呢??
      

  5.   

    也就是应该还有至少一个if是吧,那在出了最后一个if之后有没有return?
      

  6.   

    在最后一个大括号前要有个
    retrun b;
      

  7.   


    else和if 的结构一样 不过都是在5号位置返回的。。
    只有这个if我把5个位置都写了
      

  8.   

    另外,好的习惯是给b赋值之后最后再return b
    而不是直接return b;
      

  9.   

    其实只要在函数结束的时候返回一个就行了.
    如:
            public bool yanzheng(string id, string pwd, string logintype) 
            { 
                bool b = false; 
                .........
                return b;
             }
      

  10.   

    解决了 原来不光if里要return 还要在方法结束的时候return。。
      

  11.   

    不是的,你要确保所有的情况都return了.