1. 程式人生 > >樹形結構根據某節點查詢本節點及下屬所有子節點的遞歸實現

樹形結構根據某節點查詢本節點及下屬所有子節點的遞歸實現

ret sele 單位 returns rom append app mco param

數據表中CompanyId,ParentCompany,有層級關系,樹形結構根據某節點查詢本節點及下屬所有子節點的遞歸實現如下:

        public string ids = "";

        /// <summary>
        /// 根據CompanyId 查找到子單位id
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        private string
GetCompanysIds(string CompanyId) { StringBuilder strSqlcode = new StringBuilder(); strSqlcode.Append(" select * from SystemCompany where CompanyId = @CompanyId "); SqlHelper<SystemCompany> sqlHelpercode = new SqlHelper<SystemCompany>(); Dictionary
<string, object> parameterscode = new Dictionary<string, object>(); parameterscode.Add("@CompanyId", CompanyId); SystemCompany systemcompany = sqlHelpercode.FindModel(strSqlcode.ToString(), parameterscode); string s = systemcompany.CompanyId;
string CompanysIds = GetsIds(s, systemcompany.CompanyId); return CompanysIds; } private string GetsIds(string s, string CompanyId) { List<string> companyids = new List<string>(); StringBuilder strSqlcode = new StringBuilder(); strSqlcode.Append(" select * from SystemCompany where ParentCompany = @ParentCompany "); SqlHelper<SystemCompany> sqlHelpercode = new SqlHelper<SystemCompany>(); Dictionary<string, object> parameterscode = new Dictionary<string, object>(); parameterscode.Add("@ParentCompany", CompanyId); List<SystemCompany> systemcompany = sqlHelpercode.Find(strSqlcode.ToString(), parameterscode); if (systemcompany.Count > 0) { foreach (var item in systemcompany) { s = s + ";" + item.CompanyId; ids = s; GetsIds(s, item.CompanyId); } } else { ids = s; } return ids; }

向上查找同理

樹形結構根據某節點查詢本節點及下屬所有子節點的遞歸實現