繁体   English   中英

Delphi - 清除 ADO 表

[英]Delphi - clearing ADO Tables

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

我正在使用的代码:

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

来自 borland.public.delphi.database.ado 中的 2005 线程:

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

问题:

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

tblInvoices.DeleteRecords(arAll);

但它给出了一条错误消息:“在这种情况下不允许操作”。

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

原因:

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

解决方案:

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

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

其中 CabInfo 是您的表名。

暂无
暂无

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

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