復制代碼 代碼如下:
private DataTable GetTable(int topid)
{
DataTable dt = null;
try
{
string constr = "server=.;database=tqnpc;uid=sa;pwd=sa";
string selstr = "select * from RW_工作關系 where main_id=" + topid + "";
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter da = new SqlDataAdapter(selstr, con);
dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return dt;
}
protected void MakeTree()
{
DataTable dt = GetTable(0);
try
{
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["MAIN_ID"].ToString();
tn.Value = dt.Rows[i]["REF_ID"].ToString();
tn.SelectAction = TreeNodeSelectAction.Select;
TreeView1.Nodes.Add(tn);
AddTreeNodes(int.Parse(dt.Rows[i]["REF_ID"].ToString()), int.Parse(dt.Rows[i]["REF_ID"].ToString()), tn);
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
這個方法對數據庫的結構也有一定的要求,數據庫的設計如下: