如何使用C#获取Access 2007中所有表的名称和列表的名称?
我想将表名绑定到组合框,将列名绑定到列表框。
这个简单的方法会给你一个包含所有列名称的数据表
void Main()
{
using(OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=D:\temp\temp.mdb;Persist Security Info=False;"))
{
con.Open();
DataTable schema = con.GetSchema("Columns");
foreach(DataRow row in schema.Rows)
Console.WriteLine("TABLE:" + row.Field<string>("TABLE_NAME") +
" COLUMN:" + row.Field<string>("COLUMN_NAME"));
}
}
您还可以尝试使用“表”更改“列”,以获取包含有关表的更多信息的不同数据表。(也是索引的“索引”)
这段代码是:
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.Title = "path select ";
openfiledialog1.Filter = "Access 2003 (*.mdb)|*.mdb|Access 2007|*.accdb";
if (openfiledialog1.ShowDialog() == DialogResult.OK)
{
txtpath.Text = openfiledialog1.InitialDirectory + openfiledialog1.FileName;
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openfiledialog1.FileName))
{
con.Open();
DataTable schema = con.GetSchema("Columns");
foreach (DataRow row in schema.Rows)
{
listBox1.Items.Add(row.Field<string>("COLUMN_NAME"));
cmbloadtable.Items.Add(row.Field<string>("TABLE_NAME"));
}
}
}