如何使用C#获取Access 2007中所有表的名称和列表的名称?
我想将表的名称绑定到组合框,将列名绑定到列表框。
答案 0 :(得分:3)
这个简单的方法将为您提供一个包含所有列名称的数据表
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"));
}
}
您还可以尝试使用“Tables”更改“Columns”,以获得具有更多表格信息的不同数据表。 (也是索引的“索引”)
答案 1 :(得分:0)
此代码为:
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"));
}
}
}