How to query table structures from an access database?

*爱你&永不变心* 提交于 2019-12-24 00:04:08

问题


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:


You need to use CurrentDb().TableDefs if you want to access table columns. The AllTables collection doesn't offer access to the table fields.

CurrentDb().TableDefs[0].Fields[0].Name should return the name of the first column of the first table, for example.



来源:https://stackoverflow.com/questions/54075828/how-to-query-table-structures-from-an-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!