How to query table structures from an access database?

时间:2019-01-07 13:59:03

标签: c# database ms-access odbc

I want to get the structure of all the tables and odbc-Datasources in an Access database with C#. So I tried this code:

string text="";
var tables = GetApp().CurrentData.AllTables;
for (int i = 0; i < tables.Count; i++)
{
    var currentTable = tables.Item(i);
    text = text + currentTable.FullName + Environment.NewLine;
}
MessageBox.Show(text);

This returns all the availible tablenames. But how do I get the columnnamens and types of the tables?

I tried this:

var props = currentTable.Properties;
for (int j = 0; j < props.Count; j++)
{
    var prop = props.Item(j);
    text = text + Environment.NewLine + prop.Name;
}

but it didnt work (An exception was thrown when I accessed the properties).

I tried to use a oleDB connection + GetSchema to get the table structure. But with this method I only received the "native" (or local) tables inside of the access-db. But there are also some ODBC-Linked-Tables which I am also interested.

So how is it possible to get the TableNames, ColumnNames and Columntypes in an ms-access database file?

1 个答案:

答案 0 :(得分:4)

如果要访问表列,则需要使用CurrentDb().TableDefsAllTables集合不提供对表字段的访问。

例如,

CurrentDb().TableDefs[0].Fields[0].Name应该返回第一个表的第一列的名称。