2

尝试按照 c# 代码获取数据库中的所有表名,但我收到一条错误消息:

“System.Data.OleDb.OleDbException (0x80040E14):无效的 SQL 语句;应为“DELETE”、“INSERT”、“PROCEDURE”、“SELECT”或“UPDATE”...”

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tbx_Source.Text + ";");
OleDbCommand cmd = new OleDbCommand("SHOW TABLES;", conn);
OleDbDataReader reader;

try
{
    conn.Open();
    reader = cmd.ExecuteReader();
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            cbx_Tables.Items.Add(reader.GetValue(0).ToString());
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

如何使用 OleDb 执行此类命令?

谢谢 :)

4

3 回答 3

1

由于要显示数据库中的所有表(即 mdb),您可以尝试

           DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

           DataTable userTables = null;
           using (DbConnection connection = factory.CreateConnection())
           {

               connection.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data  
               Source=C:\test\testfile.accdb;Jet OLEDB:Database Password=yourrPassword;";

                string[] restrictions = new string[4];
                restrictions[3] = "Table";

                connection.Open();

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


                foreach (DataRow item in userTables.Rows) // You can use simple 
                                                          //looping to retreive the table Name
                {



                }
            }

为我工作

于 2013-10-29T12:39:56.787 回答
1

虽然凯尔的答案是我更喜欢使用 a 的答案DataTable,但您可以通过制作命令文本来使用OleDBDataReader问题代码中的anOleDbCommand

Select TABLE_NAME From INFORMATION_SCHEMA.TABLES
于 2013-10-29T13:21:04.870 回答
0

我认为你不能。

SHOW不是数据操作语句

数据操作语言 =

SELECT ... FROM ... WHERE ...
INSERT INTO ... VALUES ...
UPDATE ... SET ... WHERE ...
DELETE FROM ... WHERE ...

数据定义语言 =

CREATE TABLE ... 
DROP TABLE ... ;
ALTER TABLE ... ADD ... INTEGER;
于 2013-10-29T12:42:18.987 回答