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

PROBLEM:

I am trying to delete all records from a TADOTable. I am using the following line of code:

tblInvoices.DeleteRecords(arAll);

But it gives an error message: "Operation is not allowed in this context".

Any idea what might be causing this?

CAUSE:

Unfortunately Microsoft never implemented that option for DeleteRecords. You will need to use a Delete query.

SOLUTION:

Instead of using the DeleteRecords, use a routine like this:

    CabInfo.Active := true;

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

    CabInfo.Active := false;

where CabInfo is your table name.