在treeview 中有根节点:root1,其下有子节点:child1,child2,child3,
其中子节点名是数据库中某个表的字段,想通过单击节点在TextBox中显示相应的信息我的程序如下:
private void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
.........
strSQL="select * from table where 字段名1='"+treeView.SelectNode.Text.ToString()+"'";
comm=new SqlCommand(strcomm,conn);
dr=comm.ExecuteReader();
while(dr.Read())
{
TextBox.Text=TextBox.Text+dr["字段名1"].ToString()+"\n";
TextBox.Text=TextBox.Text+dr["字段名2"].ToString()+"\n";
...................
}
可是当我单击根节点下第一个子节点child1时,可以正确显示,而再单击child2时,无任何信息显示,我逐步调试发现当单击child2 时运行到while(dr.Read())就跳过了,没有读到人和数据,为什么会出现这样的问题?怎么解决?
希望得到大侠们的指点。。
其中子节点名是数据库中某个表的字段,想通过单击节点在TextBox中显示相应的信息我的程序如下:
private void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
.........
strSQL="select * from table where 字段名1='"+treeView.SelectNode.Text.ToString()+"'";
comm=new SqlCommand(strcomm,conn);
dr=comm.ExecuteReader();
while(dr.Read())
{
TextBox.Text=TextBox.Text+dr["字段名1"].ToString()+"\n";
TextBox.Text=TextBox.Text+dr["字段名2"].ToString()+"\n";
...................
}
可是当我单击根节点下第一个子节点child1时,可以正确显示,而再单击child2时,无任何信息显示,我逐步调试发现当单击child2 时运行到while(dr.Read())就跳过了,没有读到人和数据,为什么会出现这样的问题?怎么解决?
希望得到大侠们的指点。。
CreateDataSet();
InitTree(myTree.Nodes, "0"); // 将数据库的内容存入DataSet做缓冲
private DataSet CreateDataSet()
{
query = "SELECT * FROM YF_Tree";
myAdapter = new OleDbDataAdapter(query, myConn);
data = new DataSet();
myAdapter.Fill(data, "tree");
return data;
}
// 从DataSet中取数据建树
// 从根节点开始递归调用显示子树
private void InitTree(TreeNodeCollection Nds,string parentId)
{
TreeNode tmpNd;
DataRow [] rows = data.Tables[0].Select("T_ParentId='" + parentId + "'"); foreach(DataRow row in rows)
{
tmpNd = new TreeNode();
tmpNd.ID = row["T_NodeId"].ToString();
tmpNd.Text = row["T_NodeName"].ToString();
tmpNd.NavigateUrl = row["T_Url"].ToString();
tmpNd.Target = row["T_Target"].ToString();
Nds.Add(tmpNd);
InitTree(tmpNd.Nodes, tmpNd.ID);
}
}
strSQL="select * from table where 字段名1='"+e.Note.Text.ToString()+"'";
试试