gpt4 book ai didi

database - 如何使用ADT表重试Delphi TAdsConnection

转载 作者:行者123 更新时间:2023-12-03 08:15:49 29 4
gpt4 key购买 nike

从中可以看到,我的成员(member)软件使用FormCreate过程中的以下代码连接到许多表:

 {Open the Sessions}
Membership.LoginPrompt := False;
Membership.Username := 'ONLINE';
Membership.Password := '#######';
Membership.ConnectPath := MembershipLocation;
Membership.IsConnected := True;
PosConnection.ConnectPath := PosLocation;
PosConnection.IsConnected := True;
Bookings.ConnectPath := BookingsLocation;
Bookings.IsConnected := True;
Local.ConnectPath := LocalLocation;
Local.IsConnected := True;

// Open all the tables
for Wk1 := 0 to ComponentCount - 1 do
begin
{Skip the Tmp / New Tables}
if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'NewTable')) then
Continue;
if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'TmpTable')) then
Continue;
{Is it a TTable}
if Components[Wk1] is TAdsTable then
TAdsTable(Components[Wk1]).Active := True;
{Is it a TwwTable}
if Components[Wk1] is TwwTable then
TwwTable(Components[Wk1]).Active := True;
{Is it a TQuery}
if Components[Wk1] is TAdsQuery then
TAdsQuery(Components[Wk1]).Active := True;
end;


{Activate the Membership Tables. This is due to passwords}
Members.Active := True;
MemTypes.Active := True;
MembersById.Active := True;
MemBookMSys.Active := True;

{Rebuild the Secondry index on the MemBook table}
if RebuildIdx = True then
begin
MemBook.Active := False;
MemBook.Exclusive := True;
MemBook.Active := True;
// Check(DbiRegenIndexes(MemBook.Handle));
MemBook.Active := False;
MemBook.Exclusive := False;
MemBook.Active := True;
end;
{Make the Table Active}
MemBook.Active := True;

有时,当服务器尚未就绪时,连接会失败,并且用户会收到Advantage错误7.xxx

我需要它重试连接多次,或者经过一定时间后再试一次。

在这种情况下,是否存在错误捕获和重试连接的标准方法?还是在经过一定时间后才重复代码?

谢谢

最佳答案

您可以使用标准的try..except处理。

function TYourDataModule.ConnectToDatabases: Boolean;
begin
Result := False;

Membership.LoginPrompt := False;
Membership.Username := 'ONLINE';
Membership.Password := '#######';
Membership.ConnectPath := MembershipLocation;
PosConnection.ConnectPath := PosLocation;
Bookings.ConnectPath := BookingsLocation;
Local.ConnectPath := LocalLocation;

// Try to make all the connections together. If any fail, we'll
// hit the except block.
try
Membership.IsConnected := True;
PosConnection.IsConnected := True;
Bookings.IsConnected := True;
Local.IsConnected := True;
except
on E: EAdsDatabaseError do
begin
// Make sure all connections are closed, in case
// one or more succeeded before a failure. We'll
// be set for next time.
Membership.IsConnected := False;
PostConnection.IsConnected := False;
Booking.IsConnected := False;
Local.IsConnected := False;
Result := False;
end;
end;
end;

然后,您的调用代码可以在循环中使用该函数,直到它返回true或超过尝试次数:
var
NumTrys: Integer;
const
MAX_TRYS = 10;
TRY_DELAY = 1000;
begin
NumTrys := 0;
while NumTrys < MAX_TRYS do
begin
if YourDataModule.ConnectToDatabases then
Break;
Inc(NumTrys);
Sleep(TRY_DELAY);
end;
if NumTrys = MAX_TRYS then
// Handle not being able to connect after all attempts.
end;

请注意,在IDE中运行时会看到关于连接失败的异常消息,而在运行时则不会。如果您不想在IDE中看到它们,则可以在“项目选项”对话框中关闭对EADSDatabaseError的处理。

关于database - 如何使用ADT表重试Delphi TAdsConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7146598/

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