gpt4 book ai didi

database - Delphi - 清除 ADO 表

转载 作者:行者123 更新时间:2023-12-04 08:13:37 25 4
gpt4 key购买 nike

因此,我在程序中创建了一个按钮,该按钮应该清除数据库中的所有表,但是在运行时单击该按钮时会出错。如何消除该错误?
我正在使用的代码:
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;
我收到的错误:
enter image description here
提前感谢所有帮助!亲切的问候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 amusing the following line of code:

tblInvoices.DeleteRecords(arAll);

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

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.

关于database - Delphi - 清除 ADO 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65821505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com