c# - 如何使用 OleDb、C# 执行 SHOW 语句?

标签 c# oledb show-sql

尝试使用以下 C# 代码来获取数据库中的所有表名, 但我收到一条错误消息:

"System.Data.OleDb.OleDbException (0x80040E14): Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or '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 执行此类命令?

谢谢:)

最佳答案

由于您想显示数据库(即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
                {



                }
            }

为我工作

关于c# - 如何使用 OleDb、C# 执行 SHOW 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658405/

相关文章:

mysql - 在 mySQL show 语句中使用 'not like'

c# - 检测 IME(输入法编辑器)是否在 Silverlight 中处于事件状态

c# - 简单注入(inject)器 - 注入(inject)容器属性

c# - 在 Visual Studio 2010 中显示不必要的用法

c# - ASP 文本框调用 javascript 函数

c# - 如何在 C# 中通过 OLEDB 在 Access SQL 通配符查询中使用文字下划线?

c# - 针对 Access 2007 进行编程?

sql-server-2005 - 不支持 ITransactionLocal 接口(interface)