怪!怪!怪! ADOConnect 出了什么问题?(150分)

  • 主题发起人 主题发起人 秦浩天
  • 开始时间 开始时间

秦浩天

Unregistered / Unconfirmed
GUEST, unregistred user!
我要作一Login Form 来登录 SQL Server7,方法如下:
在Login Form 中放ADOConnect ,ConnectionString动态生成。
当Login Form 出现时选择 OK 时不出现问题, 选择Cancel
时就会出现程序执行错误,我怀疑是ADOConnect在作怪但又
不知是什么原因,请大侠指点。

程序如下:

program Personnel;

uses
Forms,
main in 'main.pas' {frmMain},
Login in 'Login.pas' {frmLogin};

{$R *.RES}

begin
Application.Initialize;
frmLogin:=TfrmLogin.Create(Application);
frmLogin.ShowModal;
if frmLogin.ModalResult=1 then
begin
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end;
end.
--------------------------------------------------------------
unit Login;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,inifiles, Buttons, Db, ADODB, DBTables;

type
TfrmLogin = class(TForm)
Panel1: TPanel;
cmdLogin: TBitBtn;
cmdCancel: TBitBtn;
txtUserName: TEdit;
txtPassword: TEdit;
Label3: TLabel;
Label4: TLabel;
ConPsl: TADOConnection;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure cmdLoginClick(Sender: TObject);
procedure cmdCancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
CountDown:Integer;
ServerName,DatabaseName:string;
end;

var
frmLogin: TfrmLogin;

implementation

var
Inifile:TInifile;

{$R *.DFM}

procedure TfrmLogin.FormCreate(Sender: TObject);
var
FileName:string;
begin
FileName:=ExtractFilePath(paramstr(0))+'IniFile.ini';
IniFile:=TIniFile.Create(FileName);
ServerName:=IniFile.ReadString('程序参数','服务器','(local)');
DatabaseName:=IniFile.ReadString('程序参数','数据库','Database');
txtUserName.Text:=IniFile.ReadString('程序参数','用户名','Guest');
end;

procedure TfrmLogin.FormDestroy(Sender: TObject);
begin
IniFile.WriteString('程序参数','用户名',txtUserName.Text);
IniFile.Destroy;
end;

procedure TfrmLogin.FormActivate(Sender: TObject);
begin
CountDown:=3;
end;

procedure TfrmLogin.cmdLoginClick(Sender: TObject);
begin
ModalResult:=mrNone;
ADOConnect.ConnectionString:='Provider=SQLOLEDB.1;'+
'Persist Security Info=False;'+
'User ID='+Trim(txtUserName.Text)+
';Initial Catalog='+Trim(DatabaseName)+
';Data Source='+Trim(ServerName)+
';Password='+txtPassword.Text;
try
ADOConnect.Open;
except
on Exception do
if not( ConPsl.Connected ) then
begin
dec(CountDown);
if CountDown<=0 then ModalResult:=mrCancel else
begin
MessageBeep($FFFF);
Application.MessageBox('请您重试一次!','无效注册信息',MB_ICONSTOP+MB_OK);
end;
exit;
end;
end;
modalResult:=mrOk;
end;

procedure TfrmLogin.cmdCancelClick(Sender: TObject);
begin
frmLogin.ModalResult:=mrCancel;
end;

end.

--------------------------------------------------------------
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons,shellapi, ComCtrls,dbtables,BDE;

type
TfrmMain = class(TForm)
Panel1: TPanel;
cmdClose: TBitBtn;
procedure cmdCloseClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.cmdCloseClick(Sender: TObject);
begin
Application.Terminate;
end;

end.
 
是不是应该稍微改动一下:

begin
Application.Initialize;
frmLogin:=TfrmLogin.Create(Application);
if frmLogin.ShowModal=mrOK then
begin
Application.CreateForm(TfrmMain, frmMain);
end;
Application.Run;
end.
 
接受答案了.
 

Similar threads

I
回复
0
查看
763
import
I
I
回复
0
查看
740
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
751
SUNSTONE的Delphi笔记
S
后退
顶部