下面这个写法得到的结果不正确,各位帮忙看看,谢谢public static int GetStaffNumByUpLeaderId(int UpleaderId, int parentDeptId)
        {
            int countStaffNum = 0;
            StringBuilder strSql1 = new StringBuilder();
            if (UpleaderId > 0)
            {
                strSql1.Append("select deptId,parentDeptId,deptUpLeaderId FROM Department");
                strSql1.Append(" where parentDeptId=" + parentDeptId);
                strSql1.Append(" and deptUpLeaderId=" + UpleaderId);
            }
            else
            {
                strSql1.Append("select deptId FROM Department");
                strSql1.Append(" where parentDeptId=" + parentDeptId);
            }
            DataTable dt1 = DbHelperSQL.Query(strSql1.ToString()).Tables[0];
            if (dt1.Rows.Count>0)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    StringBuilder strSql2 = new StringBuilder();
                    strSql2.Append("select count(userId) FROM UserInfo");
                    strSql2.AppendLine(" where deptId=" + int.Parse(dt1.Rows[i]["deptId"].ToString()));
                    object objDeptStaffNum = DbHelperSQL.GetSingle(strSql2.ToString());
                    countStaffNum += int.Parse(objDeptStaffNum.ToString());
                    countStaffNum += GetStaffCountByUpLeaderId(int.Parse(dt1.Rows[i]["deptId"].ToString()), 0);//统计下级部门员工(递归)
                }
            }
            return countStaffNum;
        }

解决方案 »

  1.   

    countStaffNum 提升为全局变量int countStaffNum = 0;
         public static int GetStaffNumByUpLeaderId(int UpleaderId, int parentDeptId)
            {
                
                StringBuilder strSql1 = new StringBuilder();
                if (UpleaderId > 0)
                {
                    strSql1.Append("select deptId,parentDeptId,deptUpLeaderId FROM Department");
                    strSql1.Append(" where parentDeptId=" + parentDeptId);
                    strSql1.Append(" and deptUpLeaderId=" + UpleaderId);
                }
                else
                {
                    strSql1.Append("select deptId FROM Department");
                    strSql1.Append(" where parentDeptId=" + parentDeptId);
                }
                DataTable dt1 = DbHelperSQL.Query(strSql1.ToString()).Tables[0];
                if (dt1.Rows.Count>0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        StringBuilder strSql2 = new StringBuilder();
                        strSql2.Append("select count(userId) FROM UserInfo");
                        strSql2.AppendLine(" where deptId=" + int.Parse(dt1.Rows[i]["deptId"].ToString()));
                        object objDeptStaffNum = DbHelperSQL.GetSingle(strSql2.ToString());
                        countStaffNum += int.Parse(objDeptStaffNum.ToString());
                        countStaffNum += GetStaffCountByUpLeaderId(int.Parse(dt1.Rows[i]["deptId"].ToString()), 0);//统计下级部门员工(递归)
                    }
                }
                return countStaffNum;
            }