【发布时间】:2013-04-12 18:17:34
【问题描述】:
Access 2007中如何用C#获取所有表名和列表名?
我想将表名绑定到组合框,将列名绑定到列表框。
【问题讨论】:
-
你碰运气了吗?
标签: c# winforms ms-access-2007
Access 2007中如何用C#获取所有表名和列表名?
我想将表名绑定到组合框,将列名绑定到列表框。
【问题讨论】:
标签: c# winforms ms-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"));
}
}
}
【讨论】: