看看~~~需要你的帮忙~~关于application.Terminate 的问题

mdejtod 2006-05-12 01:37:52
我用INI配置文件进行登录数据库,在数据模块中进行INI的验证,当登录不了系统,执行application.Terminate ,然后又有一次连接,因为我的登录窗体中也要先用到数据模块中的连接,这样会进行两次的验证,第二次时就不再执行application.Terminate 了,程序一直运行~~为什么呢???
...全文
187 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mdejtod 2006-05-12
  • 打赏
  • 举报
回复
谢谢楼上的大哥~~我回去研究礞研究一下!
postren 2006-05-12
  • 打赏
  • 举报
回复
给你写了个简单的例子

program Project1;

uses
Forms,
Windows,
Controls,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {frmLogin},
Unit3 in 'Unit3.pas' {DataModule3: TDataModule};

{$R *.res}

var
frm: TfrmLogin;

begin
Application.Initialize;
Application.CreateForm(TDataModule3, DataModule3); //数据库连接
frm := TfrmLogin.Create(nil);
if frm.ShowModal = mrOk then
begin
frm.Free;
Application.CreateForm(TForm1, Form1);
Application.Run;
end else
begin
frm.Free;
MessageBox(Application.Handle, '登陆错误', 'Error', MB_OK or MB_ICONERROR);
end;
end.
-------------------------------------
登陆窗体的单元
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TfrmLogin = class(TForm)
btnLogin: TButton;
procedure btnLoginClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

implementation

{$R *.dfm}

procedure TfrmLogin.btnLoginClick(Sender: TObject);
begin
if {登陆成功}True then //在这儿判断登陆是否成功
Self.ModalResult := mrOk
else
Self.ModalResult := mrAbort;
end;

end.
mdejtod 2006-05-12
  • 打赏
  • 举报
回复
我试过了 xuzj2000(米泥)的方法了哦,虽然不能登录系统,可是达不到效果,登录窗体一直会出现,有什么好的办法,现在发现程序会执行application.terminate了,但是有没有什么办法不让它执行两次判断,只要提示一次连接出错就好了~~
mdejtod 2006-05-12
  • 打赏
  • 举报
回复
我只是想让程序运行时提示是否连接成功,在登录窗体时只是引用了这个连接的.可是为什么第二次的时候就不会再执行结束进程了呢?.我试试三楼大哥的办法看看
postren 2006-05-12
  • 打赏
  • 举报
回复
这样你的Datamodule1不是创建了两遍?

工程文件中DataModule1有创建吗?
米泥 2006-05-12
  • 打赏
  • 举报
回复
在TADOConnection.Connected的帮助中没有说若连接不上会抛出异常呀?
ADOConnection1.ConnectionString:=s1;
ADOConnection1.Connected:=true;
建议改为:
ADOConnection1.ConnectionString:=s1;
ADOConnection1.Open;
if not ADOConnection1.Connected then
begin
application.MessageBox('数据库连接失败,请检查配置文件','连接错误',mb_iconstop+MB_OK);
application.Terminate;
end;


mdejtod 2006-05-12
  • 打赏
  • 举报
回复
谢谢楼上的大哥~~可是我有点看不明白,你帮我看下原码行么~?
procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
addsini:Tinifile;
s1,user,database,server,password:string;
if_windows_login:boolean;
begin
addsini:=Tinifile.Create(extractfilepath('d:\润丰房产\data\')+'login.ini');
try
user:=addsini.ReadString('Database','User','');
database:=addsini.ReadString('Database','database','');
server:=addsini.ReadString('Database','server','');
password:=addsini.ReadString('Database','password','');
if_windows_login:=addsini.ReadBool('database','if_windows_login',false);
finally
addsini.Free;
end;
ADOConnection1.Connected:=false;
try
begin
if if_windows_login then
s1:='Provider=SQLOLEDB.1;'+
'Integrated Security=SSPI;Persist Security Info=False;'+
'Initial Catalog='+database+';Data Source='+server+''
else
S1:='Provider=SQLOLEDB.1;'+
'Password='+password+';'+
'Persist Security Info=False;'+
'User ID='+user+';'+
'Initial Catalog='+database+';'+
'Data Source='+Server+';'+
'Use Procedure for Prepare=1;'+
'Auto Translate=True;'+
'Packet Size=4096;'+
'Workstation ID=hx05;'+
'Use Encryption for Data=False;'+
'Tag with column collation when possible=False';
end;
ADOConnection1.ConnectionString:=s1;
ADOConnection1.Connected:=true;
except
application.MessageBox('数据库连接失败,请检查配置文件','连接错误',mb_iconstop+MB_OK);
application.Terminate;

end;
end;

end.
还有一个就是登录窗体上有这句话: ado_tmp.Connection:=datamodule1.ADOConnection1;
程序主窗体上有这句话:procedure TE_MainF.FormCreate(Sender: TObject); //ONCREATE事件触发登录界面窗体
begin
datamodule1:=tdatamodule1.Create(self);
if not assigned(E_loginF) then
E_loginF:=TE_loginF.create(self);
E_loginF.ShowModal;
也就是会进行两次连接,不懂为什么第二次的时候怎么就不执行application.terminate了
postren 2006-05-12
  • 打赏
  • 举报
回复
这样写
program Project1;

uses
Forms,
Windows,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm2, Form2); //登陆窗体
if 登陆成功 then
begin
Application.CreateForm(TForm1, Form1); //主窗体
Application.Run;
end else
begin
MessageBox(Application.Handle, '登陆错误', PChar(Application.Title), MB_OK or MB_ICONERROR);
end;
end.

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧