Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 检查表是否为空(MSAccess数据库+Delphi)_Database_Delphi_Ms Access_Is Empty - Fatal编程技术网

Database 检查表是否为空(MSAccess数据库+Delphi)

Database 检查表是否为空(MSAccess数据库+Delphi),database,delphi,ms-access,is-empty,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.m

我需要确定创建的表中是否有条目

我需要的是

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

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

在成功执行select语句后运行此操作

能否对表select count1 from tablename发出SQL select,并查看它返回的记录数?您没有将Adotable设置为active,因此您将始终获得true。顺便说一句,如果您使用ADOTable1.ConnectionString,则不需要使用ADOTable1.ConnectionString。我得到的结果是true而不是false。@bummi。我需要检查数据库中是否存在帐户,因此如果使用select count1 from tablename,我需要使用一些额外的空间?不是吗?如果你不打算实际使用你阅读的内容,你应该使用SELECT TOP 1*。这将阻止您实际传输表中可能包含数百万条记录的所有数据。如果您知道表中的主键字段名,请使用*的istead使其更快…我已将修改后的代码添加到主帖子中。它总是返回false。当您处于调试模式时,RecordCount有哪个值?@Priyabrata您能告诉我们为什么您的函数不起作用,但答案起作用了吗?我运行的查询是错误的。@bummi和一些关于isEmpty如何不受影响的问题,在查询原因之后我不知道。因为您声明此函数总是返回false!!在你的编辑中,我仍然看不出答案和你修改过的代码有什么区别。