How to catch errors when a database connection fails, avoiding pop-up errors so that you can reconnect automatically the next time
The main is to avoid manual intervention. Once the error message pops up, if there is no manual intervention to turn it off, the program will stop there and not be able to reconnect automatically.
While Try....except protection is added to the connection, there is no way to avoid the sudden disconnection of the network connection when data is being manipulated in the database, or the error prompt when the database service is closed.
I've seen it can be handled in an event in application, but I can't retrieve it now.
LZ said that the event is applicataion.onexception, the program all errors will trigger this event
The code for a system and database connection can be many and exist in different units
Each open/excesql/... Plus try...except is obviously not a good way to achieve, you find ways to write a method can be generic, and then for other units to call
Then only in this method try...except on the line, the connection failed to lock the program again start detection, how long or how many times also the connection is not successful to quit the program, but to distinguish between SQL statement syntax error or ADO connection failure, you can nest a try...except, such as:
try
adoquery1
.
open;
except
on
e:exception
do
begin
showmessage(
‘执行语句失败,原因:‘
+e
.
message);
try
adoconnection1
.
Connected:=
false
;
adoconnection1
.
Connected:=
true
;
except
on
e:exception
do
begin
{showmessage(‘连接数据库失败,原因:‘+:e.message);
这里调用另一方法启动检测(方法内是循环/延时}
end
;
end
;
end
;
end
;
This should be handled in a database operation function, through the try except end to catch the database connection error message, if the database operation is wrong or the connection fails, then rusult:=e.message, if there is no error result:= " , the return value can handle whether the database needs to be reconnected, give you a piece of code to see:
{初始化时Timer的enable为false}
procedure
TDataModule1
.
TimerTimer(Sender:TObject);
begin
OpenLink;
{用户重新连接数据库}
end
;
procedure
TDataModule1
.
OpenLink;
var
ini:Tinifile;
s,DSource,dbname,uname,pword:
string
;
begin
Timer
.
Enabled:=
false
;
ledform
.
label1
.
Caption:=
‘正在重新连接数据库......‘
;
ini:=Tinifile
.
Create(extractfilepath(paramstr(
0
))+
‘DataConnect.ini‘
);
try
DSource:=ini
.
ReadString(
‘DataBase‘
,
‘Server‘
,
‘192.168.0.1‘
);
dbname:=ini
.
ReadString(
‘DataBase‘
,
‘DB‘
,
‘Message‘
);
uname:=ini
.
ReadString(
‘DataBase‘
,
‘UName‘
,
‘user‘
);
pword:=ini
.
ReadString(
‘DataBase‘
,
‘PW‘
,
‘12345‘
);
finally
ini
.
Free;
end
;
s:=
‘Provider=SQLOLEDB.1;Persist Security Info=True;‘
;
s:=s+
‘Data Source=‘
+DSource;
s:=s+
‘;User ID=‘
+uname;
s:=s+
‘;Password=‘
+pword;
s:=s+
‘;Initial Catalog=‘
+dbname;
try
ADOConnection1
.
Close;
ADOConnection1
.
ConnectionString:=s;
ADOConnection1
.
Open;
ledform
.
label1
.
Visible:=
false
;
except
ledform
.
label1
.
Caption:=‘连接数据库失败!
10
秒后重试!‘;
Timer
.
Interval:=
10000
;
Timer
.
Enabled:=
true
;
end
;
end
;
{数据库操作}
function
TDataModule1
.
GetSD(Sid:
Integer
;ppn:
Integer
):
string
;
begin
try
with
WSD
do
begin
Close;
SQL
.
Clear ;
SQL
.
Add(
‘select * from led_kongtiao_View‘
);
SQL
.
Add(
‘ where ITEM_ID=‘
+inttostr(Sid));
Open;
if
RecordCount>
0
then
begin
if
(FieldByName(
‘ITEM_VALUE‘
).IsNull)
or
(FieldByName(
‘ITEM_VALUE‘
).AsString=
‘‘
)
then
SD[ppn]:=
‘---‘
else
SD[ppn]:=Dl(FieldByName(
‘ITEM_VALUE‘
).AsFloat)+
‘%‘
;
end
;
end
;
except
on
E: Exception
do
Result := E
.
Message;
end
;
end
;
{判断数据库操作是否成功}
function
Tledform
.
GetDB:
string
;
var
str:
string
;
begin
Result:=
‘‘
;
str:=DataModule1
.
GetSD(
1
,
1
);
if
str<>
‘‘
then
{重新连接数据库,具体可根据自己的实际情况来处理!}
DataModule1
.
timer
.
enable:=
true
;
else
{正常连接数据库,无操作}
end
;
How to catch errors when a database connection fails, avoiding pop-up errors so that you can reconnect automatically the next time