如何使用 ADO 从 MS Access 数据库中提取表名

发布于 2024-10-07 07:01:25 字数 519 浏览 3 评论 0原文

我尝试使用这段代码:

OleDbConnection c = new OleDbConnection(con);
string SQLS = "SELECT MSysObjects.Name FROM MSysObjects WHERE MSysObjects.Name Not Like 'MsyS*' AND MSysObjects.Type=1 ORDER BY MSysObjects.Name";
OleDbDataAdapter da = new OleDbDataAdapter(SQLS, c);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

但我得到了这个异常:

无法读取记录;没有“MSysObjects”的读取权限。

现在,我需要以编程方式将整个 ms-access 数据库传输到 mysql,因此我需要数据库名称。我该如何解决这个错误?

i tried using this code :

OleDbConnection c = new OleDbConnection(con);
string SQLS = "SELECT MSysObjects.Name FROM MSysObjects WHERE MSysObjects.Name Not Like 'MsyS*' AND MSysObjects.Type=1 ORDER BY MSysObjects.Name";
OleDbDataAdapter da = new OleDbDataAdapter(SQLS, c);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

But i got this exception:

Record(s) cannot be read; no read permission on 'MSysObjects'.

Now, i need to transfer the entire ms-access database to mysql programmaticaly, thus i need the database names. How do I work my way around this error?

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

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

发布评论

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

评论(3

追风人 2024-10-14 07:01:25
using System;
using System.Data;
using System.Data.OleDb;

public class DatabaseInfo {    
    public static void Main () { 
        String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
        OleDbConnection con = new OleDbConnection(connect);
        con.Open();  
        Console.WriteLine("Made the connection to the database");

        Console.WriteLine("Information for each table contains:");
        DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});

        Console.WriteLine("The tables are:");
            foreach(DataRow row in tables.Rows) 
                Console.Write("  {0}", row[2]);


        con.Close();
    }
}

///取自
http://www.java2s.com/Code/CSharp/Database -ADO.net/Getalltablenames.htm

using System;
using System.Data;
using System.Data.OleDb;

public class DatabaseInfo {    
    public static void Main () { 
        String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
        OleDbConnection con = new OleDbConnection(connect);
        con.Open();  
        Console.WriteLine("Made the connection to the database");

        Console.WriteLine("Information for each table contains:");
        DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});

        Console.WriteLine("The tables are:");
            foreach(DataRow row in tables.Rows) 
                Console.Write("  {0}", row[2]);


        con.Close();
    }
}

///taken from
http://www.java2s.com/Code/CSharp/Database-ADO.net/Getalltablenames.htm

二货你真萌 2024-10-14 07:01:25

您可以这样访问它:

OleDbConnection conn =
 new OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
    "C:\\phycoaide\\phycoaide.mdb;Persist Security Info=False;");

// retrieving schema for a single table
OleDbCommand cmd = new OleDbCommand("taxa", conn);
cmd.CommandType = CommandType.TableDirect;
conn.Open();
OleDbDataReader reader =
 cmd.ExecuteReader(CommandBehavior.SchemaOnly);
DataTable schemaTable = reader.GetSchemaTable();
reader.Close();
conn.Close();

请参阅 http://harborsparrow.blogspot.com/2009/05/c-code-to-get-schema-of-access-table.html 了解更多详细信息。

编辑:
好的,那么您可以使用如下解决方案检索所有表: 如何在 C# 中使用 OleDB 列出 MS Access 文件中的所有查询?,然后循环遍历它们。

You can access it like this:

OleDbConnection conn =
 new OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
    "C:\\phycoaide\\phycoaide.mdb;Persist Security Info=False;");

// retrieving schema for a single table
OleDbCommand cmd = new OleDbCommand("taxa", conn);
cmd.CommandType = CommandType.TableDirect;
conn.Open();
OleDbDataReader reader =
 cmd.ExecuteReader(CommandBehavior.SchemaOnly);
DataTable schemaTable = reader.GetSchemaTable();
reader.Close();
conn.Close();

See http://harborsparrow.blogspot.com/2009/05/c-code-to-get-schema-of-access-table.html for more details.

EDIT:
Ok, so then you can retrieve all the tables using a solution like this: How do I list all the queries in a MS Access file using OleDB in C#? and then loop through them.

自我难过 2024-10-14 07:01:25

您可以尝试 Kros.Utils.MsAccess。此软件包支持 MsAccess 数据库架构。

You can try Kros.Utils.MsAccess. This package has support for MsAccess database schema.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文