using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace APP_tree { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataSet ds = new DataSet(); private void Form1_Load(object sender, EventArgs e) { TreeNode tn = new TreeNode("表"); treeView1.Nodes.Add(tn); TreeNode tnNode; OleDbConnection odConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=library.mdb;"); OleDbCommand odComm = new OleDbCommand(); odComm.Connection = odConn; OleDbDataAdapter odda = new OleDbDataAdapter(odComm); foreach (string tableName in new string[] { "reader", "book", "load" }) { odComm.CommandText = "select * from" + tableName; odConn.Open(); odda.Fill(ds,tableName); odConn.Close(); tnNode = new TreeNode(tableName); tn.Nodes.Add(tnNode); for (int i = 0; i < ds.Tables[tableName].Columns.Count; i++) { TreeNode tn1 = new TreeNode(ds.Tables[tableName].Columns[i].ColumnName); tnNode.Nodes.Add(tn1); } } } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { string strTableName = e.Node.Text; dataGridView1.DataSource = ds.Tables[strTableName]; } } }
from后面少了一个空格
"select * from " + tableName
嗯,现在可以了,搞了我几个钟,谢谢了
from reader
谢谢