您的位置:首页 > 其它

递归获取子结点(包括自身结点)

2007-11-23 17:40 176 查看
#region 递归获取子结点(包括自身结点)

/// <summary>

/// 递归获取子结点(包括自身结点)

/// </summary>

/// <param name="values">当前结点值</param>

/// <param name="sql">查询所有记录的Sql语句</param>

/// <param name="ParentFieldName">父ID字段名</param>

/// <param name="KeyFieldName">主键字段名</param>

/// <returns>主键字符串</returns>

/// <example>

/// private string GetChildrenDeparment(string departmentid)

///{

/// string sql = "select * from Department";

/// string DepartmentID = "DepartmentParent";

/// string KeyFieldName = "DepartmentID";

/// return GetChildren(departmentid, sql, ParentFieldName, KeyFieldName);

///}

/// </example>

public static string GetChildren(string values, string sql, string ParentFieldName, string KeyFieldName)

{

//根据Sql获得DataTable

DataTable source = GetDataTableBySql(sql);

//初始化结果字符串

string result = "";

//递归获取子串其它本身

result = GetChildren(values, source, ParentFieldName, KeyFieldName);

return result.Substring(0, result.Length - 1);

}

/// <summary>

/// 递归获取子结点(包括自身结点)

/// </summary>

/// <param name="values">当前结点值</param>

/// <param name="source">包括所有数据的数据源</param>

/// <param name="ParentFieldName">父ID字段名</param>

/// <param name="KeyFieldName">主键字段名</param>

/// <returns>主键字符串</returns>

private static string GetChildren(string values, DataTable source, string ParentFieldName, string KeyFieldName)

{

string children = values + ",";

DataRow[] results = source.Select(ParentFieldName + "='" + values + "'");

if (results.Length != 0)

{

foreach (DataRow dr in results)

{

children += GetChildren(dr[KeyFieldName].ToString().Trim(), source, ParentFieldName, KeyFieldName);

}

}

return children;

}

#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐