我在treeview为什么只显示了数据库的第一条记录,其他为什么没有显示出来
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private const string Stock_Image_Url = "images/mediastock.gif";
private const string Sort_Image_Url = "images/mediastock.gif";
private System.Windows.Forms.TreeView tvw;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
this.CreateTreeView();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void CreateTreeView()
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\TaxLaw\\TaxLaw\\feitiumjoy.mdb";
tvw.Nodes.Clear() ;
string sql = "SELECT * FROM Type ";
OleDbConnection conn = new OleDbConnection() ;
conn.ConnectionString = connStr ;
OleDbDataAdapter oleDa = new OleDbDataAdapter(sql,connStr);
DataSet ds = new DataSet();
oleDa.Fill(ds,"type");
oleDa.SelectCommand.CommandText = "SELECT * FROM SmallClass";
oleDa.Fill(ds,"smallclass");
conn.Close();
DataColumn primaryKey = ds.Tables["SmallClass"].Columns["SmallClassID"];
ds.Tables["smallclass"].PrimaryKey = new DataColumn[]{primaryKey};
TreeNode node = GetRootNode(ds.Tables["type"],ds.Tables["smallclass"]);
if(node != null)
tvw.Nodes.Add(node);
node.Expand();
}
private TreeNode GetRootNode(DataTable dtType,DataTable dtSmallClass)
{
//调用函数前必须检查是否有记录
if(dtType.Rows.Count > 0)
{
TreeNode stockNode = new TreeNode();
stockNode.Text = dtType.Rows[0]["TypeName"].ToString();
stockNode.SelectedImageIndex = 1;
return stockNode;
}
else
{
return null;
}
}
}
}