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

标签 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!!

最佳答案

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

在成功执行 select 语句后运行此命令

关于database - 检查表是否为空(MSAccess 数据库 + Delphi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354219/

相关文章:

c# - 如何在同一个 ExecuteScalar() 命令中同时检查多个相等的记录

ruby-on-rails - 如何为测试目的模拟数据库故障(在 Ruby on Rails 中)

mysql - 带有消息框的数据库设计问题

delphi - .NET 4.0 中的 P/Invoke 环境是否发生了变化?

java - 使用自动编号字段插入到 Access 表中

node.js - 在文档/行达到一定年龄后在数据库上运行查询

delphi - Delphi IDE是用什么IDE开发的?

delphi - DWScript 是否兼容为 Android 和 IO 创建 Delphi XE5 代码?

c# - 如何将 MY SQL 查询的输出转换为 Access 数据库

windows - 如何在 Windows 中从命令行为 *.accdb 创建用户 DSN?