datamoudle在连接数据库异常后,如何退出主程序。见所列出代码!(50分)

狼牙

Unregistered / Unconfirmed
GUEST, unregistred user!
datamoudle在连接数据库异常后,如何退出主程序?
经查,倘若连接服务器地址不对的话,将弹出“连接数据库错误,请确定连接数据库正常!”
对话框,点击确定后,窗体消失,但在系统进程中,依然存在,因为其是datamoudle窗体,
没有继承form,所以无法用application.Terminate。请问,如何在出现该异常后,退出程序?
见所列出代码!
procedure TDM.DataModuleCreate(Sender: TObject);
var
MyIni:Tinifile;
i:integer;
begin
MyIni:= TIniFile.Create(Fmain.GetExePath+'config.ini');
try
ADOConnection1.ConnectionString:=MyIni.ReadString('Config','Cstr','error');
try
ADOConnection1.Connected:=true;
except
raise Exception.CreateFmt('%s 连接数据库错误,请确定连接数据库正常!',['错误!']);
exit;
end;
with Fmain do
begin
if ADOConnection1.Connected then
try
ADOTable1.Open;
ADOTable2.Open;
except
raise Exception.CreateFmt('%s 连接数据表打开错误!',['错误!']);
exit;
end;
end;
finally
MyIni.Free;
end;
end;
 
不要让Delphi帮你自动写创建TDM实例的代码,自己写,加上Try finally end控制,在Finally中释放创建的TDM实例。
 
如果是自动创建,请列出代码。
如果是手动创建,也请举出代码。
 
自动创建的:
Application.CreateForm(TDM, DM);
手动创建的:(.PRJ文件中)
DM:=TDM.Create(Application);
try
Application.Run;
finally
DM.Free;
End.
 
试一下 Halt(0);行吗?
 
to 解元:
倒,不是这个意思,呵呵。我说的手动创建代码和自动创建代码是指如何弹出异常后,
自动退出程序。呵呵。
 
在DPR文件中,只自动创建两个FORM,主和datamodule,其余的动态创建。
DPR文件示例:
const ApplicationName = 'TFrmMain';

var
Start : TfrmStart;
HWD : Integer;

begin
Application.Title := '远程传输系统';
HWD := FindWindow(ApplicationName,nil);
if HWD <> 0 then
begin
MessageBox(0,'该程序已经有一个在运行中!' + #13 + ' 无法运行第二实例','提示',0);
// Halt;
end;
Application.Initialize;
Start := TfrmStart.Create(Application);
try
Start.Show;
Start.Update;
Application.CreateForm(TDMShare, DMShare);
Application.CreateForm(TFrmMain, FrmMain);
finally
Start.Free;
end;
Application.Run;
end.
DATAMODULE的oncreate事件:
procedure TDMShare.DataModuleCreate(Sender: TObject);
begin
GetDataBaseSetup;
try
tabReceipt.Active := TRUE;
...
except
MessageBox(0,'用户数据库连接失败,程序退出!','提示',0);
halt;
if tabReceipt.Active then
tabReceipt.Active := FALSE;
Application.Terminate ;
end;
if tabReceipt.Active then
tabReceipt.Active := FALSE;
GetDefaultVal;
end;
 
to armyjinag
你的和我的没有本质的区别阿。我的目的是要求在datamoudle的create事件中能准确地
弹出相应地错误,比如“该服务器不存在,用户名错误等等”,然后再退出整个系统。
在datamoudle中,是无法使用application对象的阿,所以你的行不通。
to lb_icesea79
halt(0)没有作用。进程依然存在。
 
有个halt在!
你想完全捕获错误,这可得试一试。看具体的错误代码能否提练出来。
 
procedure TDM.DataModuleCreate(Sender: TObject);
var
MyIni:Tinifile;
i:integer;
begin
MyIni:= TIniFile.Create(Fmain.GetExePath+'config.ini');
try
ADOConnection1.ConnectionString:=MyIni.ReadString('Config','Cstr','error');
try
ADOConnection1.Connected:=true;
except
raise Exception.CreateFmt('%s 连接数据库错误,请确定连接数据库正常!',['错误!']);
Application.Terminate ;/////---------加上这句
exit;
end;
with Fmain do
begin
if ADOConnection1.Connected then
try
ADOTable1.Open;
ADOTable2.Open;
except
raise Exception.CreateFmt('%s 连接数据表打开错误!',['错误!']);
exit;
end;
end;
finally
MyIni.Free;
end;
end;
 
to svw056:
Application对象需要有forms单元才能存在,而单纯的datamoudle窗体没有继承forms,
所以,无法调用该对象。
halt,也不起作用,难道真的只有在mainform中的create事件中,判断
adoconnection.connected= ture吗?不会吧,应该有更好的办法吧。
 
实在要用Application对象的话,可以在DataModule单元引用Forms单元。
 
to 解元:
Tdatamoudle不是继承至form,即使添加了forms单元,同样是不起作用的。
 
我不太清楚在你的机器上出现了什么错误!
但是我贴出来的代码是我现在在用的代码,可以达到你的目标。
就是数据库的错误无法得到详细的信息。
 
顶部