简体   繁体   English

使用 Delphi XE3 如何在 MS Access 中获取表列表并排除视图/查询?

[英]Using Delphi XE3 How Do I get a List Of Tables in MS Access AND Exclude Views / Queries?

Using Delphi XE3 How Do I get a List Of Tables in MS Access AND Exclude Views / Queries ?使用 Delphi XE3 如何在 MS Access 中获取表列表并排除视图/查询?

I have tried using ADOConnection1.GetTableNames - but it returns ALL Tables and Views (Queries).我曾尝试使用 ADOConnection1.GetTableNames - 但它返回所有表和视图(查询)。

I have also tried using a query eg.我也尝试过使用查询,例如。 "SELECT * FROM MSysObjects WHERE Type=1 AND Flags=0" which requires a System.mdw file, however this causes more issues as a mdw file does not always exist. “SELECT * FROM MSysObjects WHERE Type=1 AND Flags=0”需要 System.mdw 文件,但这会导致更多问题,因为 mdw 文件并不总是存在。

I am wanting to develop an application that compares the Table structures of TWO mdb files, and creates a script to modify / synch the table structures within a database.我想开发一个应用程序来比较两个 mdb 文件的表结构,并创建一个脚本来修改/同步数据库中的表结构。

Any help greatly appreciated.非常感谢任何帮助。

You can use the OpenSchema method passing the siTables value.您可以使用传递siTables值的OpenSchema方法。 And then filter the result using the TABLE_TYPE column of the returned dataset.然后使用返回数据集的TABLE_TYPE列过滤结果。

Try this sample code试试这个示例代码

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Adodb,
  Variants;


procedure ListTables(const FileName : string);
var
  LADOConnection : TADOConnection;
  LADODataSet: TADODataSet;

begin
  LADOConnection := TADOConnection.Create(nil);
  try
    LADOConnection.ConnectionString := Format('Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s;Persist Security Info=False;', [FileName]);
    LADODataSet := TADODataSet.Create(nil);
    try
     LADODataSet.Connection := LADOConnection;
     LADOConnection.OpenSchema(siTables, EmptyParam, EmptyParam, LADODataSet);

     LADODataSet.Filter   := '(TABLE_TYPE = ''TABLE'')'; //show only the tables
     LADODataSet.Filtered := True;

     while not LADODataSet.EOF do
     begin
      Writeln(Format('Name %s Type %s',[LADODataSet.FieldByName('TABLE_NAME').AsString, LADODataSet.FieldByName('TABLE_TYPE').AsString]));

      LADODataSet.Next;
     end;

    finally
     LADODataSet.Free;
    end;
  finally
    LADOConnection.Free;
  end;
end;

begin
 try
    CoInitialize(nil);
    try
       ListTables('C:\Test\Northwind.MDB');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

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

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