检查表是否为空(MS Access Database + Delphi)

时间:2013-10-14 05:54:40

标签: database delphi ms-access is-empty

我需要找出创建的表中是否包含条目。

我需要的是,

if (TableIsEmpty) then
     do_something
else
     do_something_else;

我为此目的写的是:

Function IsTableEmpty:Boolean;
Var
  DataSource : string;
Begin
  DataSource :=
     'Provider=Microsoft.Jet.OLEDB.4.0'+
     ';Data Source=c:\mydb.mdb'+
     ';Persist Security Info=False';

  Form2.ADOConnection1.ConnectionString := DataSource;
  Form2.ADOConnection1.LoginPrompt := False;
  Form2.ADOCommand1.Connection := Form2.ADOConnection1;
  Form2.ADOTable1.ConnectionString := DataSource;
  Form2.ADOTable1.Connection := Form2.ADOConnection1;
  if (Form2.ADOTable1.IsEmpty)then
      result := true
  else
      result := false;
End;

但是无论表的状态如何,此函数都返回true!

编辑*** 修改后的代码:

Function IsTableEmpty:Boolean;
Var
  DataSource, cs : string;
Begin
  DataSource :=
     'Provider=Microsoft.Jet.OLEDB.4.0'+
     ';Data Source=c:\Users.mdb'+
     ';Persist Security Info=False';

  Form2.ADOConnection1.ConnectionString := DataSource;
  Form2.ADOConnection1.LoginPrompt := False;
  Form2.ADOCommand1.Connection := Form2.ADOConnection1;
  Form2.ADOTable1.Connection := Form2.ADOConnection1;
  Form2.ADOTable1.TableName := 'userIdentification';
  Form2.ADOTable1.Active := True;
  cs := 'Select * from userIdentification';
  Form2.ADOCommand1.CommandText := cs;
  Form2.ADOCommand1.Execute;
  if Form2.ADOTable1.RecordCount <= 0 then
     result := true
  else
     result := false;
  Form2.ADOConnection1.Close;
End;

此函数始终返回false !!

1 个答案:

答案 0 :(得分:5)

if Form2.ADOTable1.RecordCount =< 0 then
     do_something
else
     do_something_else;

在成功执行的select语句

之后运行此命令