我有一个数据库表(名为 PatientDetails),其中包括以下列:
PatientId not null,
title,
sex,
lastname,
birthday,
firstname,
middlename,
remarkline,
remarks
通过使用上表,我想在 c# 中将PatientId
asParent
和 childnode 作为firstname
a TreeView
。我怎样才能做到这一点 ?
我做了一些代码隐藏如下:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = RunQuery("select PatientId,firstname from PatientDetails");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode root = new TreeNode(
ds.Tables[0].Rows[i][1].ToString(),
ds.Tables[0].Rows[i][0].ToString());
root.SelectAction = TreeNodeSelectAction.Expand;
CreateNode(root);
TVPatArc.Nodes.Add(root);
}
}
void CreateNode(TreeNode node)
{
DataSet ds = RunQuery("Select PatientId,firstname "
+ "from PatientDetails where PatientId ="
+ node.Value);
if (ds.Tables[0].Rows.Count == 0)
{
return;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode tnode = new TreeNode(
ds.Tables[0].Rows[i][1].ToString(),
ds.Tables[0].Rows[i][0].ToString());
tnode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(tnode);
CreateNode(tnode);
}
}
DataSet RunQuery(String Query)
{
DataSet ds = new DataSet();
String connStr = "Data Source=LOCALHOST\\SQLEXPRESS;"
+ "Initial Catalog=MyDatabase;"
+ "User ID=sa;Password=sa";
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand objCommand = new SqlCommand(Query, conn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
da.Fill(ds);
da.Dispose();
}
return ds;
}
但是CreateNode(tnode);
它给出了System.StackOverflowException
. 我认为这是由于无限循环。但我不确定。