主题:一个delphi连接SQLServer 2000的问题,请高手指教
用delphi连接SQLServer 2000数据库,运行时弹出对话框如此下:
Project Project1.exe raised exception class EAccessViolation with message
'Access violation at address 00499D3f in module'Project1.exe'.Read of address 00000058'.
Break Continue Help
请问为何故?
数据模块单元代码如下:
unit uData;
interface
uses
SysUtils, Classes, DB, ADODB;
type
TDataModulePer = class(TDataModule)
ADOConnectionPer: TADOConnection;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModulePer: TDataModulePer;
implementation
{$R *.dfm}
end.
登录窗口单元代码如下:
unit uLogin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,uData,DB,ADODB;
type
TuLoginForm = class(TForm)
Label1: TLabel;
UserNameLabel: TLabel;
UserPwdLabel: TLabel;
cmbUserName: TComboBox;
edtUserPwd: TEdit;
okBtn: TButton;
exitBtn: TButton;
procedure okBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
uLoginForm: TuLoginForm;
implementation
{$R *.dfm}
procedure TuLoginForm.FormCreate(Sender: TObject);
Var
Query:TADOQuery;
begin
cmbUserName.Clear;
try
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModulePer.ADOConnectionPer;
Query.SQL.Clear;
Query.SQL.Add('select USER_NAME from operator');
Query.Open;
if Query.RecordCount=0 then
begin
MessageDlg('没有可以登录的用户名',mtError,[mbOK],0);
exit;
end
else
begin
Query.First;
while Query.Eof=false do
begin
cmbUserName.Items.Add(Query.FieldByName('USER_NAME').AsString);
Query.Next;
end;
end;
Query.Close;
Query.Free;
cmbUserName.ItemIndex:=0;
except
MessageDlg('连接数据库出错!',mtError,[mbOk],0);
end;
end;
procedure TuLoginForm.okBtnClick(Sender: TObject);
Var
Query:TADOQuery;
begin
try
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModulePer.ADOConnectionPer;
Query.SQL.Clear;
Query.SQL.Add('select *from operator where USER_NAME=:USERNAME and USER_PWD=:USERPWD');
Query.Parameters.ParamByName('USERNAME').Value:=cmbUserName.Text;
Query.Parameters.ParamByName('USERPWD').Value:=edtUserPwd.Text;
Query.Open;
if Query.RecordCount=0 then
begin
MessageDlg('请输入正确的用户名和密码',mtError,[mbOK],0);
exit;
end;
if Query.FieldByName('CLASS').AsInteger=0 then
begin
end
else
begin
end;
edtUserPwd.Text:='';
Query.Close;
Query.Free;
except
MessageDlg('连接数据库出错!',mtError,[mbOk],0);
end;
end;
end.
Project Project1.exe raised exception class EAccessViolation with message
'Access violation at address 00499D3f in module'Project1.exe'.Read of address 00000058'.
Break Continue Help
请问为何故?
数据模块单元代码如下:
unit uData;
interface
uses
SysUtils, Classes, DB, ADODB;
type
TDataModulePer = class(TDataModule)
ADOConnectionPer: TADOConnection;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModulePer: TDataModulePer;
implementation
{$R *.dfm}
end.
登录窗口单元代码如下:
unit uLogin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,uData,DB,ADODB;
type
TuLoginForm = class(TForm)
Label1: TLabel;
UserNameLabel: TLabel;
UserPwdLabel: TLabel;
cmbUserName: TComboBox;
edtUserPwd: TEdit;
okBtn: TButton;
exitBtn: TButton;
procedure okBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
uLoginForm: TuLoginForm;
implementation
{$R *.dfm}
procedure TuLoginForm.FormCreate(Sender: TObject);
Var
Query:TADOQuery;
begin
cmbUserName.Clear;
try
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModulePer.ADOConnectionPer;
Query.SQL.Clear;
Query.SQL.Add('select USER_NAME from operator');
Query.Open;
if Query.RecordCount=0 then
begin
MessageDlg('没有可以登录的用户名',mtError,[mbOK],0);
exit;
end
else
begin
Query.First;
while Query.Eof=false do
begin
cmbUserName.Items.Add(Query.FieldByName('USER_NAME').AsString);
Query.Next;
end;
end;
Query.Close;
Query.Free;
cmbUserName.ItemIndex:=0;
except
MessageDlg('连接数据库出错!',mtError,[mbOk],0);
end;
end;
procedure TuLoginForm.okBtnClick(Sender: TObject);
Var
Query:TADOQuery;
begin
try
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModulePer.ADOConnectionPer;
Query.SQL.Clear;
Query.SQL.Add('select *from operator where USER_NAME=:USERNAME and USER_PWD=:USERPWD');
Query.Parameters.ParamByName('USERNAME').Value:=cmbUserName.Text;
Query.Parameters.ParamByName('USERPWD').Value:=edtUserPwd.Text;
Query.Open;
if Query.RecordCount=0 then
begin
MessageDlg('请输入正确的用户名和密码',mtError,[mbOK],0);
exit;
end;
if Query.FieldByName('CLASS').AsInteger=0 then
begin
end
else
begin
end;
edtUserPwd.Text:='';
Query.Close;
Query.Free;
except
MessageDlg('连接数据库出错!',mtError,[mbOk],0);
end;
end;
end.