如何获取 Access (Jet) 数据库中的表列表?

发布于 2024-11-10 02:39:53 字数 88 浏览 7 评论 0原文

我需要查看我的 c# 程序使用的 Access 数据库中是否存在表。我们知道其他数据库的 SQL 命令将返回表列表。 Access/Jet 数据库有这样的命令吗?

I need to see if a table exists in an Access database used by my c# program. Is know there are SQL commands for other databases that will return a list of tables. Is there such a command for Access/Jet databases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单调的奢华 2024-11-17 02:39:53

尝试 GetSchema()

    connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\access.mdb";    

    connection.Open();

    DataTable userTables = connection.GetSchema("Tables");

Try the GetSchema()

    connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\access.mdb";    

    connection.Open();

    DataTable userTables = connection.GetSchema("Tables");
憧憬巴黎街头的黎明 2024-11-17 02:39:53

完整代码: 获取 Access 数据库中的表列表 - ADO.NET教程

// Microsoft Access provider factory
DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb");

DataTable userTables = null;

using (DbConnection connection =
            factory.CreateConnection())
{
    // c:\test\test.mdb
    connection.ConnectionString = "Provider=Microsoft
        .Jet.OLEDB.4.0;Data Source=c:\\test\\test.mdb";

    // We only want user tables, not system tables
    string[] restrictions = new string[4];
    restrictions[3] = "Table";

    connection.Open();

    // Get list of user tables
    userTables =
        connection.GetSchema("Tables", restrictions);
}

// Add list of table names to listBox
for (int i=0; i < userTables.Rows.Count; i++)
    listBox1.Items.Add(userTables.Rows[i][2].ToString())

这里是你的答案:http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/d2eaf851-fc06-49a1-b7bd-bca76669783e

Full code : Get List of Tables in an Access Database - ADO.NET Tutorials

// Microsoft Access provider factory
DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb");

DataTable userTables = null;

using (DbConnection connection =
            factory.CreateConnection())
{
    // c:\test\test.mdb
    connection.ConnectionString = "Provider=Microsoft
        .Jet.OLEDB.4.0;Data Source=c:\\test\\test.mdb";

    // We only want user tables, not system tables
    string[] restrictions = new string[4];
    restrictions[3] = "Table";

    connection.Open();

    // Get list of user tables
    userTables =
        connection.GetSchema("Tables", restrictions);
}

// Add list of table names to listBox
for (int i=0; i < userTables.Rows.Count; i++)
    listBox1.Items.Add(userTables.Rows[i][2].ToString())

here is answer for you : http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/d2eaf851-fc06-49a1-b7bd-bca76669783e

江挽川 2024-11-17 02:39:53

像这样的事情应该可以解决问题。子句Type = 1 指定表。请注意,这还将包括结果集中的系统表(它们以前缀“MSys”开头)。

SELECT Name FROM MSysObjects WHERE Type = 1

Something like this should do the trick. The clause Type = 1 specifies tables. Note that this will also include the system tables in the result set (they start with the prefix "MSys".

SELECT Name FROM MSysObjects WHERE Type = 1
撩动你心 2024-11-17 02:39:53

那个对我有用

using (OleDbConnection con = new OleDbConnection(connectionString))
            {
                con.Open();
                DataTable dt = con.GetSchema("Tables");
                var selectNames = dt.Rows.Cast<DataRow>().Where(c => !c["TABLE_NAME"].ToString().Contains("MSys")).ToArray();
                foreach (var item in selectNames)
                {
                     // add names to comboBox
                    comboBox1.Items.Add(item["TABLE_NAME"]);
                }
            }

that one worked for me

using (OleDbConnection con = new OleDbConnection(connectionString))
            {
                con.Open();
                DataTable dt = con.GetSchema("Tables");
                var selectNames = dt.Rows.Cast<DataRow>().Where(c => !c["TABLE_NAME"].ToString().Contains("MSys")).ToArray();
                foreach (var item in selectNames)
                {
                     // add names to comboBox
                    comboBox1.Items.Add(item["TABLE_NAME"]);
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文