繁体   English   中英

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

[英]How do I extract table names from MS Access database using ADO

我尝试使用此代码:

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,因此我需要数据库名称。 如何解决该错误?

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

您可以像这样访问它:

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文件中列出所有查询? 然后遍历它们。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM