【问题标题】:Delphi - clearing ADO TablesDelphi - 清除 ADO 表
【发布时间】:2021-01-21 05:11:00
【问题描述】:

所以我在我的程序中创建了一个按钮,该按钮应该清除数据库中的所有表,但在运行时单击该按钮时会出错。如何消除该错误?

我正在使用的代码:

ClearDB 按钮的代码

procedure TfrmEntry.bmbClearDBClick(Sender: TObject);
var
  i:integer;
begin
  i:=MessageDlg('Are you sure you want to clear the Racers database? (all current data in the database will be lost.)',mtWarning,[mbOK,mbCancel],0);
  if i = mrOk then
  begin
    //clears entire database
    with dmRacers do
    begin
      tbl1660.DeleteRecords(arAll);
      tblXKarts.DeleteRecords(arAll);
      tblTwoPointOne.DeleteRecords(arAll);
      tblMidB.DeleteRecords(arAll);
      tblMidA.DeleteRecords(arAll);
      tblLateModel.DeleteRecords(arAll);
      tblSprints.DeleteRecords(arAll);
      tblV8.DeleteRecords(arAll);
      tblHeavyMetals.DeleteRecords(arAll);
      tblHotrods.DeleteRecords(arAll);
      tblPinkrods.DeleteRecords(arAll);
      tblStockrods.DeleteRecords(arAll);
      tblMinis.DeleteRecords(arAll);
      tblDevelopment.DeleteRecords(arAll);
    end;
  end
  else
  begin
    i:=MessageDlg('Clear aborted',mtInformation,[mbOk],0);
  end;
end;

输入按钮的代码

procedure TfrmEntry.btnEntryClick(Sender: TObject);
  var
  sRacerName,sLicence,sCarNum:string;
  iNum:integer;
begin
//saves input (works perfectly)
  sCarNum:=edtCarNumber.Text;
  sRacerName:=edtRacerName.Text;
  sLicence:=edtLicenseNum.Text;

            //ifs for saving input to the db
    if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = '1660s' then
    begin
       with dmRacers do
       begin
          tbl1660.insert;
          tbl1660['Car Number']:=sCarNum;
          tbl1660['Racer Name']:=sRacerName;
          tbl1660['Licence']:=sLicence;
          tbl1660.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = '2.1s' then
    begin
              with dmRacers do
       begin
          tblTwoPointOne.insert;
          tblTwoPointOne['Car Number']:=sCarNum;
          tblTwoPointOne['Racer Name']:=sRacerName;
         tblTwoPointOne['Licence']:=sLicence;
          tblTwoPointOne.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Crosskarts' then
    begin
              with dmRacers do
       begin
           tblXKarts.insert;
          tblXKarts['Car Number']:=sCarNum;
         tblXKarts['Racer Name']:=sRacerName;
          tblXKarts['Licence']:=sLicence;
          tblXKarts.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Heavy Metals' then
    begin
              with dmRacers do
       begin
           tblHeavyMetals.insert;
          tblHeavyMetals['Car Number']:=sCarNum;
         tblHeavyMetals['Racer Name']:=sRacerName;
          tblHeavyMetals['Licence']:=sLicence;
         tblHeavyMetals.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Hotrods' then
    begin
              with dmRacers do
       begin
        tblHotrods.insert;
          tblHotrods['Car Number']:=sCarNum;
         tblHotrods['Racer Name']:=sRacerName;
          tblHotrods['Licence']:=sLicence;
         tblHotrods.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Midgets A' then
    begin
               with dmRacers do
       begin
        tblMidA.insert;
          tblMidA['Car Number']:=sCarNum;
         tblMidA['Racer Name']:=sRacerName;
          tblMidA['Licence']:=sLicence;
         tblMidA.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Midgets B' then
    begin
               with dmRacers do
       begin
        tblMidB.insert;
          tblMidB['Car Number']:=sCarNum;
         tblMidB['Racer Name']:=sRacerName;
          tblMidB['Licence']:=sLicence;
         tblMidB.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Minis' then
    begin
               with dmRacers do
       begin
           tblMinis.insert;
          tblMinis['Car Number']:=sCarNum;
         tblMinis['Racer Name']:=sRacerName;
          tblMinis['Licence']:=sLicence;
          tblMinis.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Pinkrods' then
    begin
               with dmRacers do
       begin
           tblPinkrods.insert;
          tblPinkrods['Car Number']:=sCarNum;
         tblPinkrods['Racer Name']:=sRacerName;
          tblPinkrods['Licence']:=sLicence;
          tblPinkrods.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Sprints' then
    begin
               with dmRacers do
       begin
           tblSprints.insert;
          tblSprints['Car Number']:=sCarNum;
          tblSprints['Racer Name']:=sRacerName;
          tblSprints['Licence']:=sLicence;
          tblSprints.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Stockrods' then
    begin
               with dmRacers do
       begin
           tblStockrods.insert;
          tblStockrods['Car Number']:=sCarNum;
          tblStockrods['Racer Name']:=sRacerName;
          tblStockrods['Licence']:=sLicence;
          tblStockrods.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'SWD Development' then
    begin
                with dmRacers do
       begin
          tblDevelopment.insert;
          tblDevelopment['Car Number']:=sCarNum;
          tblDevelopment['Racer Name']:=sRacerName;
          tblDevelopment['Licence']:=sLicence;
          tblDevelopment.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'V8s' then
    begin
               with dmRacers do
       begin
          tblV8.insert;
          tblV8['Car Number']:=sCarNum;
          tblV8['Racer Name']:=sRacerName;
          tblV8['Licence']:=sLicence;
          tblV8.Post;
       end;
    end
    else if cbxGridSelect.Items[cbxGridSelect.ItemIndex] = 'Late Models' then
         begin
                  with dmRacers do
       begin
          tblLateModel.insert;
          tblLateModel['Car Number']:=sCarNum;
          tblLateModel['Racer Name']:=sRacerName;
          tblLateModel['Licence']:=sLicence;
          tblLateModel.Post;
       end;
         end;
  end;

数据模块的代码

   const
  scConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%pathtomdb%Racers.mdb;Mode=ReadWrite;Persist Security Info=False;';

procedure TdmRacers.DataModuleCreate(Sender: TObject);
var
path:string;
begin
  path:=ExtractFilePath(ParamStr(0));
  conToDB.ConnectionString := StringReplace(scConnectionString, '%pathtomdb%', path, []);
  conToDB.Connected:=True;
  tbl1660.Active := True;
  tblXKarts.Active := True;
  tblTwoPointOne.Active := True;
  tblMidB.Active := True;
  tblMidA.Active := True;
  tblLateModel.Active := True;
  tblSprints.Active := True;
  tblV8.Active := True;
  tblHeavyMetals.Active := True;
  tblHotrods.Active := True;
  tblPinkrods.Active := True;
  tblStockrods.Active := True;
  tblMinis.Active := True;
  tblDevelopment.Active := True;
end;

我收到错误:

提前感谢所有帮助!
亲切的问候
PrimeBeat

【问题讨论】:

  • 始终指出错误最终发生在哪个源代码行。这意味着,进入函数/过程调用,直到找到实际触发错误的代码行。在调试器中,您使用F8 跳过函数/过程调用,并使用F7 进入它们。
  • 我的代码第 87 行是:MyCmd.CommandText := 'Delete * from tbl1660';
  • 好!所以(参考你对你得到的一个答案的评论),MyCmd nil?也许你没有意识到你需要创建它,就像任何对象一样?
  • @TomBrunberg 好吧,我没想到。我应该在.Create() 命令的括号之间放什么?因为如果我插入nilSelf,它仍然会导致访问冲突?
  • 如果你写了例如MyCmd.Create(nil),那就错了。正确的方法是MyCmd := TADOCommand.Create(nil)。但是,让我们退后一步。当您使用 ADO 时,我希望您在表单中添加了 TADOCommand(默认名为 ADOCommand1)。如果你已经这样做了,那么你已经有了一个命令组件,只需将你得到的答案中的代码中的MyCmd 替换为ADOCommand1,因为ADOCommand1 将被表单自动实例化。

标签: database ms-access delphi delphi-xe2


【解决方案1】:

来自 borland.public.delphi.database.ado 中 2005 年的一个帖子:

http://www.devsuperpage.com/search/Articles.asp?ArtID=877427

问题:

我正在尝试从 TADOTable 中删除所有记录。我是 使用以下代码行:

tblInvoices.DeleteRecords(arAll);

但它给出了一个错误消息:“在此不允许操作 上下文”。

知道是什么原因造成的吗?

原因:

不幸的是,Microsoft 从未为 DeleteRecords 实现该选项。 您将需要使用删除查询。

解决方案:

不要使用 DeleteRecords,而是使用如下例程:

    CabInfo.Active := true;

    MyCmd.CommandText := 'Delete * from CabInfo';
    MyCmd.Execute;

    CabInfo.Active := false;

其中 CabInfo 是您的表名。

【讨论】:

  • 它不起作用,它给出了访问冲突错误。我把它放在一个程序中,我就像你在你的答案中一样使用它,但它只会给出错误。
  • 具体错误是“模块'frmRacerEntry_p.exe'中地址0069C483的访问冲突。读取地址00000000”。我很快就会从数据模块中添加我的代码,也许我有一个我没有看到的问题。
  • @PrimeBeat AV 地址为零或接近零通常表示尝试访问尚未创建的对象。同样,错误发生在代码的哪一行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多