从一个ODBC数据库中的所有表和所有列 [英] Get all tables and all columns from a odbc database

查看:238
本文介绍了从一个ODBC数据库中的所有表和所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想所有的表的名字来自一个OdbcConnection,并为所有的表的名字,我想接受她所有的列名。

I want to get all "table" names from a OdbcConnection, and for all "table" names I want to recieve all column names.

所以,我遇到了 OdbcConnection.GetSchema() functionallity。我Manges律师通过简单地使用 connection.GetSchema(表)让所有的表名。但现在我想要得到的列信息的表。我注意到 connection.GetSchema(列)给我列的信息,但这只是给了它从一个随机/第一(?)中的数据源表(用CSV的Windows驱动程序),这不利于非常mutch。

So I came across the OdbcConnection.GetSchema() functionallity. I manges to get all the table names by simply using connection.GetSchema("Tables"). But now I want to get the column information for those tables. I noticed connection.GetSchema("Columns") will give me columns information, but this only gives it from a random/first (?) "table" in the datasource (using Windows CSV driver), which doesn't help very mutch.

最具挑战性的部分是,将与任何(大部分)ODBC驱动程序的工作。我不知道哪个底层数据源将被使用。

The most challenging part is, that would have to work with any (most) ODBC drivers. I won't know which underlying datasource will be used.

任何想法?

推荐答案

列架构将返回所有表

cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
       new Object[] { null, null, null, null });

或单个表

cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
       new Object[] { null, null, "table1", null });

同样,

columns = cn.GetSchema("Columns");

返回所有表中的所有列。

Returns all columns in all tables.

更多信息:模式限制

编辑回复意见

    string cs = @"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=z:\docs;";
    OdbcConnection cn = new OdbcConnection(cs);
    cn.Open();

    DataTable tables = cn.GetSchema("Tables");
    DataTable columns = cn.GetSchema("Columns");

    foreach (DataRow row in columns.Rows)
    {
        Console.WriteLine(row["COLUMN_NAME"].ToString());
        Console.WriteLine(row["TABLE_NAME"].ToString());
    }
    Console.Read();

这篇关于从一个ODBC数据库中的所有表和所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆