Windows 窗体应用程序从数据库表加载树并将其显示在 TreeView 对象中?
这是我到目前为止的代码,我的循环有问题,我不明白。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication11_TreeView
{
public partial class Form1 : Form
{
OleDbConnection dbConn;
public Form1()
{
InitializeComponent();
string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; DataSource=PartsTree.accdb";
try
{
dbConn = new OleDbConnection(connStr);
dbConn.Open();
AddChildNodes(treeView1.Nodes, 0);
dbConn.Close();
dbConn.Dispose();
}
catch (OleDbException e)
{
MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
private void AddChildNodes(TreeNodeCollection nodes, int parent)
{
string queryStr = "SELECT ID, parent_ID, description";
queryStr += "FROM parts Where parent_ID";
queryStr += (0 == parent ? "IS NULL;" : "=?");
OleDbCommand dbCmd = dbConn.CreateCommand();
dbCmd.CommandText = queryStr;
if (0 != parent)
{
OleDbParameter parameter = dbCmd.Parameters.Add("@InputParm", OleDbType.Integer);
parameter.Value = parent;
}
using (OleDbDataReader rdr = dbCmd.ExecuteReader())
{
while (rdr.Read())
{
}
rdr.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
This is the Code I have so far, I am having problem with the loop, I don't get it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication11_TreeView
{
public partial class Form1 : Form
{
OleDbConnection dbConn;
public Form1()
{
InitializeComponent();
string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; DataSource=PartsTree.accdb";
try
{
dbConn = new OleDbConnection(connStr);
dbConn.Open();
AddChildNodes(treeView1.Nodes, 0);
dbConn.Close();
dbConn.Dispose();
}
catch (OleDbException e)
{
MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
private void AddChildNodes(TreeNodeCollection nodes, int parent)
{
string queryStr = "SELECT ID, parent_ID, description";
queryStr += "FROM parts Where parent_ID";
queryStr += (0 == parent ? "IS NULL;" : "=?");
OleDbCommand dbCmd = dbConn.CreateCommand();
dbCmd.CommandText = queryStr;
if (0 != parent)
{
OleDbParameter parameter = dbCmd.Parameters.Add("@InputParm", OleDbType.Integer);
parameter.Value = parent;
}
using (OleDbDataReader rdr = dbCmd.ExecuteReader())
{
while (rdr.Read())
{
}
rdr.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是递归方法的示例:
This is an example of a recursive method: