如何从Access数据库查询表结构? [英] How to query table structures from an access database?
问题描述
我想使用C#获取Access数据库中所有表和odbc-Datasources的结构.所以我尝试了这段代码:
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?
我尝试过:
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).
我试图使用oleDB连接+ GetSchema来获取表结构.但是通过这种方法,我只在access-db内收到本地"(或本地)表.但是也有一些我也感兴趣的ODBC链接表.
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.
那么如何在ms-access数据库文件中获取TableNames,ColumnNames和Columntype?
So how is it possible to get the TableNames, ColumnNames and Columntypes in an ms-access database file?
推荐答案
如果要访问表列,则需要使用CurrentDb().TableDefs
. AllTables
集合不提供对表字段的访问.
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
应该返回第一个表的第一列的名称.
CurrentDb().TableDefs[0].Fields[0].Name
should return the name of the first column of the first table, for example.
这篇关于如何从Access数据库查询表结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!