秦
秦浩天
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.
在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.