/// <summary>
/// 返回一个DataSet
/// </summary>
/// <param name="cmdStr"></param>
/// <returns></returns>
public DataSet GetDataSet(string cmdStr)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=MyTest;Integrated Security=True";
con.Open();
SqlCommand selectCMD = new SqlCommand(cmdStr,con);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS);
con.Close();
return custDS;
}
/// <summary>
/// 产生根树
/// </summary>
public TreeNode ParentTree()
{
String str = "select * from Tree where parentID = 0 ";
DataSet ds = GetDataSet(str);
TreeNode parentNode = new TreeNode();
foreach (DataRow dr in ds.Tables[0].Rows)
{
parentNode.Text = dr["Name"].ToString();
parentNode.NavigateUrl = "#";
ChildTree(parentNode, dr["childID"].ToString());
parentNode.Expanded = false;
}
return parentNode;
}
/// <summary>
/// 遍历子树
/// </summary>
/// <param name=" parentNode"></param>
/// <param name="childID"></param>
public void ChildTree(TreeNode parentNode, string childID)
{
String str = "select * from Tree where parentID=" + Convert.ToInt32(childID) + "";
DataSet ds = GetDataSet(str);
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode childNode = new TreeNode();
parentNode.ChildNodes.Add(childNode);
childNode.Text = dr["Name"].ToString();
childNode.Value = dr["childID"].ToString();
childNode.NavigateUrl = "NewPage.aspx";
ChildTree(childNode, dr["childID"].ToString());
childNode.Expanded = false;
}
}