我用ADOConnection控件来连接SQL数据库,编译通过了,可是在用登陆窗口登陆时出现这个错误 "project database.exe raise exception class EAccessViolation with message 'Accessviolation at address 00486EA1 in module database.exe'.Read of address 000002FC'.Process stopped"。想问下这个是怎么回事?代码如下:
unit userver;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ExtCtrls,ShellAPI, Menus, DB, DBTables,INIFiles, ADODB;
const
CM_RESTORE=WM_USER+$1000;
WZGL_APP_NAME='WZGL_System';
WM_WZGLNOTIFY=WM_USER+100;
strNotifyTip='物资管理应用服务器';
ID_MAIN=100;
type
TfrmMain = class(TForm)
PopupMenu: TPopupMenu;
NshowForm: TMenuItem;
NClose: TMenuItem;
ADOConnection: TADOConnection;
procedure NCloseClick(Sender: TObject);
procedure FormDestroy(sender:Tobject);
procedure NshowFormClick(Sender: TObject);
procedure Formcreate(Sender: TObject); private
{ Private declarations }
public
procedure Createparams(var params:TCreateparams);override;
procedure Restorerequest(var message:TMessage);message CM_RESTORE;
procedure Notify(var Msg:TMessage);message WM_WZGLNOTIFY;
procedure minimize(sender:Tobject);
function DBConnect(aServerName,aDatabaseName,aUserName,aPassword:string):boolean;
{ Public declarations }
end;var
frmMain: TfrmMain;
nid:NotifyIconData;implementation
uses
BDConnectFrm;
procedure TfrmMain.Createparams(var params:TCreateparams);
begin
inherited createparams(Params);
params.winclassname:=WZGL_APP_NAME;
end;
procedure TfrmMain.restorerequest(var message:TMessage);
begin
if isiconic(Application.Handle)=true then
application.restore
else
application.bringtofront;
end; {$R *.dfm}procedure TfrmMain.NCloseClick(Sender: TObject);
begin
close;
end;
procedure TfrmMain.Notify(var Msg:TMessage);
var
pt:TPoint;
begin
case msg.LParam of
WM_RBUTTONDOWN:
begin
SetForeGroundWindow(nid.Wnd);
GetCursorPos(pt);
Popupmenu.Popup(pt.X,pt.Y);
end;
WM_LBUTTONDOWN:
begin
application.Minimize;
frmMain.Hide;
end;
WM_LBUTTONDBLCLK:
begin
showwindow(application.Handle,SW_SHOWNORMAL);
end;
end;
end;
procedure TfrmMain.minimize(sender:Tobject);
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);
nid.Wnd:=handle;
nid.uID:=ID_MAIN;
nid.hIcon:=Application.Icon.Handle;
strcopy(nid.szTip,strnotifytip);
nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
nid.uCallbackMessage:=WM_WZGLNOTIFY;
Shell_NotifyIcon(NIM_ADD,@nid);
showwindow(application.Handle,SW_HIDE);
end;
Procedure TfrmMain.Formcreate(sender:Tobject);
var
serverName,databaseName,userName,password:string;
begin
ReadFormIni(serverName,databaseName,userName,password);
if not DBConnect(ServerName,DatabaseName,UserName,Password) then
if not DBConnectExecute(TfrmDBConnect) then
application.Terminate;
application.OnMinimize:=minimize;
end;
procedure TfrmMain.FormDestroy(sender:Tobject);
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);
nid.Wnd:=Handle;
nid.uID:=ID_MAIN;
nid.uFlags:=0;
Shell_NotifyIcon(NIM_DELETE,@nid);
end;
procedure TfrmMain.NshowFormClick(Sender: TObject);
begin
showwindow(application.Handle,SW_SHOWNORMAL);
end;
function TfrmMain.DBConnect(aServerName,aDatabaseName,aUserName,aPassword:string):boolean;
begin
result:=true;
ADOConnection.Connected:=false;
ADOConnection.ConnectionString:='Provider=SQLOLEDB.1'+';Persist Security Info=true'+';User ID='+aUserName+';password='+aPassword+';Initial Catalog='+aDatabaseName+';Data Source='+aServerName;
try
ADOConnection.Connected:=true;
except
result:=false;
end;
end;
end.
unit BDConnectFrm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,INIFiles;type
TfrmDBConnect = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
Button2: TButton;
EdtServerName: TEdit;
edtDatabaseName: TEdit;
EdtUserName: TEdit;
EdtPassword: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure ReadFormini(var aServerName,aDatabaseName,aUserName,aPassword:string);
procedure WriteToini(const aServerName,aDatabaseName,aUserName,aPassword:string);
function DBConnectExecute(aClass:TFormClass):boolean;
var
frmDBConnect: TfrmDBConnect;
connected:boolean;implementation
uses
userver;{$R *.dfm}function DBConnectExecute(aClass:TFormClass):boolean;
begin
try
frmMain.ShowModal;
finally
frmMain.free;
result:=connected;
end;
end;
procedure ReadFormini(var aServerName,aDatabaseName,aUserName,aPassword:string);
var
IniFile:TIniFile;
FileName:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'\DB.ini';
IniFile:=TInifile.Create(FileName);
aServerName:=IniFile.ReadString('DB','ServerName','');
aDatabaseName:=IniFile.ReadString('DB','DatabaseName','');
aUserName:=Inifile.ReadString('DB','UserName','');
aPassword:=Inifile.ReadString('DB','Password','');
Inifile.Free;
end;
procedure WriteToini(const aServerName,aDatabaseName,aUserName,aPassword:string);
var
IniFile:TIniFile;
FileName:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'\DB.ini';
IniFile:=TInifile.Create(FileName);
IniFile.WriteString('DB','ServerName',aServerName);
IniFile.WriteString('DB','DatabaseName',aDatabaseName);
IniFile.WriteString('DB','UserName',aUserName);
IniFile.WriteString('DB','Password',aPassword);
IniFile.Free;
end;
procedure TfrmDBConnect.Button1Click(Sender: TObject);
begin
WriteToini(edtServerName.Text,edtDatabaseName.Text,edtUserName.Text,edtPassword.Text);
if frmMain.dbconnect(edtServerName.Text,edtDatabaseName.Text,edtUserName.Text,edtPassword.Text) then
begin
application.MessageBox('数据库连接成功','提示',Mb_IconInformation+Mb_Okcancel);
Connected:=true;
close;
end
else
begin
application.MessageBox('数据库连接失败,请确认后再输入','提示',Mb_Iconwarning+mb_ok);
end;
end;procedure TfrmDBConnect.Button2Click(Sender: TObject);
begin
close;
end;end.
unit userver;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ExtCtrls,ShellAPI, Menus, DB, DBTables,INIFiles, ADODB;
const
CM_RESTORE=WM_USER+$1000;
WZGL_APP_NAME='WZGL_System';
WM_WZGLNOTIFY=WM_USER+100;
strNotifyTip='物资管理应用服务器';
ID_MAIN=100;
type
TfrmMain = class(TForm)
PopupMenu: TPopupMenu;
NshowForm: TMenuItem;
NClose: TMenuItem;
ADOConnection: TADOConnection;
procedure NCloseClick(Sender: TObject);
procedure FormDestroy(sender:Tobject);
procedure NshowFormClick(Sender: TObject);
procedure Formcreate(Sender: TObject); private
{ Private declarations }
public
procedure Createparams(var params:TCreateparams);override;
procedure Restorerequest(var message:TMessage);message CM_RESTORE;
procedure Notify(var Msg:TMessage);message WM_WZGLNOTIFY;
procedure minimize(sender:Tobject);
function DBConnect(aServerName,aDatabaseName,aUserName,aPassword:string):boolean;
{ Public declarations }
end;var
frmMain: TfrmMain;
nid:NotifyIconData;implementation
uses
BDConnectFrm;
procedure TfrmMain.Createparams(var params:TCreateparams);
begin
inherited createparams(Params);
params.winclassname:=WZGL_APP_NAME;
end;
procedure TfrmMain.restorerequest(var message:TMessage);
begin
if isiconic(Application.Handle)=true then
application.restore
else
application.bringtofront;
end; {$R *.dfm}procedure TfrmMain.NCloseClick(Sender: TObject);
begin
close;
end;
procedure TfrmMain.Notify(var Msg:TMessage);
var
pt:TPoint;
begin
case msg.LParam of
WM_RBUTTONDOWN:
begin
SetForeGroundWindow(nid.Wnd);
GetCursorPos(pt);
Popupmenu.Popup(pt.X,pt.Y);
end;
WM_LBUTTONDOWN:
begin
application.Minimize;
frmMain.Hide;
end;
WM_LBUTTONDBLCLK:
begin
showwindow(application.Handle,SW_SHOWNORMAL);
end;
end;
end;
procedure TfrmMain.minimize(sender:Tobject);
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);
nid.Wnd:=handle;
nid.uID:=ID_MAIN;
nid.hIcon:=Application.Icon.Handle;
strcopy(nid.szTip,strnotifytip);
nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
nid.uCallbackMessage:=WM_WZGLNOTIFY;
Shell_NotifyIcon(NIM_ADD,@nid);
showwindow(application.Handle,SW_HIDE);
end;
Procedure TfrmMain.Formcreate(sender:Tobject);
var
serverName,databaseName,userName,password:string;
begin
ReadFormIni(serverName,databaseName,userName,password);
if not DBConnect(ServerName,DatabaseName,UserName,Password) then
if not DBConnectExecute(TfrmDBConnect) then
application.Terminate;
application.OnMinimize:=minimize;
end;
procedure TfrmMain.FormDestroy(sender:Tobject);
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);
nid.Wnd:=Handle;
nid.uID:=ID_MAIN;
nid.uFlags:=0;
Shell_NotifyIcon(NIM_DELETE,@nid);
end;
procedure TfrmMain.NshowFormClick(Sender: TObject);
begin
showwindow(application.Handle,SW_SHOWNORMAL);
end;
function TfrmMain.DBConnect(aServerName,aDatabaseName,aUserName,aPassword:string):boolean;
begin
result:=true;
ADOConnection.Connected:=false;
ADOConnection.ConnectionString:='Provider=SQLOLEDB.1'+';Persist Security Info=true'+';User ID='+aUserName+';password='+aPassword+';Initial Catalog='+aDatabaseName+';Data Source='+aServerName;
try
ADOConnection.Connected:=true;
except
result:=false;
end;
end;
end.
unit BDConnectFrm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,INIFiles;type
TfrmDBConnect = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
Button2: TButton;
EdtServerName: TEdit;
edtDatabaseName: TEdit;
EdtUserName: TEdit;
EdtPassword: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure ReadFormini(var aServerName,aDatabaseName,aUserName,aPassword:string);
procedure WriteToini(const aServerName,aDatabaseName,aUserName,aPassword:string);
function DBConnectExecute(aClass:TFormClass):boolean;
var
frmDBConnect: TfrmDBConnect;
connected:boolean;implementation
uses
userver;{$R *.dfm}function DBConnectExecute(aClass:TFormClass):boolean;
begin
try
frmMain.ShowModal;
finally
frmMain.free;
result:=connected;
end;
end;
procedure ReadFormini(var aServerName,aDatabaseName,aUserName,aPassword:string);
var
IniFile:TIniFile;
FileName:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'\DB.ini';
IniFile:=TInifile.Create(FileName);
aServerName:=IniFile.ReadString('DB','ServerName','');
aDatabaseName:=IniFile.ReadString('DB','DatabaseName','');
aUserName:=Inifile.ReadString('DB','UserName','');
aPassword:=Inifile.ReadString('DB','Password','');
Inifile.Free;
end;
procedure WriteToini(const aServerName,aDatabaseName,aUserName,aPassword:string);
var
IniFile:TIniFile;
FileName:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'\DB.ini';
IniFile:=TInifile.Create(FileName);
IniFile.WriteString('DB','ServerName',aServerName);
IniFile.WriteString('DB','DatabaseName',aDatabaseName);
IniFile.WriteString('DB','UserName',aUserName);
IniFile.WriteString('DB','Password',aPassword);
IniFile.Free;
end;
procedure TfrmDBConnect.Button1Click(Sender: TObject);
begin
WriteToini(edtServerName.Text,edtDatabaseName.Text,edtUserName.Text,edtPassword.Text);
if frmMain.dbconnect(edtServerName.Text,edtDatabaseName.Text,edtUserName.Text,edtPassword.Text) then
begin
application.MessageBox('数据库连接成功','提示',Mb_IconInformation+Mb_Okcancel);
Connected:=true;
close;
end
else
begin
application.MessageBox('数据库连接失败,请确认后再输入','提示',Mb_Iconwarning+mb_ok);
end;
end;procedure TfrmDBConnect.Button2Click(Sender: TObject);
begin
close;
end;end.
var
serverName,databaseName,userName,password:string;
begin
ReadFormIni(serverName,databaseName,userName,password);
if not DBConnect(ServerName,DatabaseName,UserName,Password) then
if not DBConnectExecute(TfrmDBConnect) then
application.Terminate;
application.OnMinimize:=minimize;
>>>end;
======================================
Procedure TfrmMain.FormActivate(sender:Tobject);
var
serverName,databaseName,userName,password:string;
begin
ReadFormIni(serverName,databaseName,userName,password);
if not DBConnect(ServerName,DatabaseName,UserName,Password) then
if not DBConnectExecute(TfrmDBConnect) then
application.Terminate;
application.OnMinimize:=minimize;
end;