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

时间:2010-12-14 19:16:10

标签: c# sql database ado.net

我尝试使用此代码:

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

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

你可以像这样访问它:

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

编辑:
好的,那么你可以使用这样的解决方案检索所有表:How do I list all the queries in a MS Access file using OleDB in C#?然后循环遍历它们。

答案 2 :(得分:0)

您可以尝试Kros.Utils.MsAccess。此软件包支持MsAccess database schema