DataModule和TadoQuery的问题? ( 积分: 50 )

  • 主题发起人 主题发起人 只你
  • 开始时间 开始时间

只你

Unregistered / Unconfirmed
GUEST, unregistred user!
unit ULogin;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB,DBTables,ADODB;

type
TFrmLogin = class(TForm)
Label1: TLabel;
Label2: TLabel;
UserName: TComboBox;
UserPwd: TEdit;
BtnDm: TBitBtn;
BrmCancel: TBitBtn;
procedure BtnDmClick(Sender: TObject);
procedure BrmCancelClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure UserPwdKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FrmLogin: TFrmLogin;

implementation
uses UDM,UMain,UIn,uOut,UKcManager;
{$R *.dfm}

procedure TFrmLogin.BtnDmClick(Sender: TObject);
var
Query : TADOQuery;
begin
try
Query := TADOQuery.Create(nil);
Query.Connection := Dm.ADOConnection1;
Query.SQL.Add('select * from Operator where UserName =:UserName and UserPwd =: UserPwd');


Query.Parameters.ParamByName('UserName').Value:=UserName.Text;
Query.Parameters.ParamByName('UserPwd').Value:=UserPwd.Text;
Query.Open;
if Query.RecordCount = 0 then
MessageDlg('请输入正确的用户和密码!!!',mtERROR,[mbok],0);
exit;
if Query.FieldByName('Class').AsInteger = 0 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

FrmMain.Show;
FrmLogin.Close;
end;
if Query.FieldByName('Class').AsInteger = 1 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

Ypin.Show ;
FrmLogin.Close;
end;
if Query.FieldByName('Class').AsInteger = 2 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

KcManager.Show;
FrmLogin.Close;
end;
if Query.FieldByName('Class').AsInteger = 3 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

YpOut.Show;
FrmLogin.Close;
end;
except
MessageDlg('数据库连接出错!!!',mtERROR,[mbOK],0);

end;

end;

procedure TFrmLogin.BrmCancelClick(Sender: TObject);
begin
modalResult := mrCancel;
FrmLogin.Close;
Application.Terminate;
end;

procedure TFrmLogin.FormShow(Sender: TObject);
begin
UserPwd.SetFocus;
end;

procedure TFrmLogin.FormCreate(Sender: TObject);
var
Query : TADOQuery;
begin
UserName.Clear;
try
Query := TADOQuery.Create(nil);
Query.Connection := Dm.ADOConnection1;
Query.SQL.Add('select username 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
UserName.Items.Add(Query.FieldByName('UserName').asstring);
Query.Next;
end;
end;
Query.Close;
query.Free;
UserName.ItemIndex := 0;
except
MessageDlg('链接数据库出错!!!',mtERROR,[mbOK],0);
end;
end;
procedure TFrmLogin.UserPwdKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = 13 then
BtnDmClick(Sender);
end;

end.

但是运行的时候老是会跳出这个错误,再按运行后也不行,那位高手帮我看看
project yiyao.exe raised exception class EAccessViolaton with message'
Access violation at address 004ef2eb in module 'yiyao.exe'.Read of address 00000058' process stopped.use
Step or Run to continue
 
是什么时候出现的错误?程序启动的时候?按按钮的时候?
楼主最好先用断点看看哪几行代码出问题,要不然很难确定问题所在。
 
Query.Connection := Dm.ADOConnection1;我运行时候就是这行出问题!
 
procedure TFrmLogin.BtnDmClick(Sender: TObject);
var
Query : TADOQuery;
begin
try
Query := TADOQuery.Create(nil);
Query.Connection := Dm.ADOConnection1;
Query.SQL.Add('select * from Operator where UserName =:UserName and UserPwd =: UserPwd');


Query.Parameters.ParamByName('UserName').Value:=UserName.Text;
Query.Parameters.ParamByName('UserPwd').Value:=UserPwd.Text;
Query.Open;
if Query.RecordCount = 0 then
begin
MessageDlg('请输入正确的用户和密码!!!',mtERROR,[mbok],0);
Query.close;Query.Free; exit;
end;
if Query.FieldByName('Class').AsInteger = 0 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

FrmMain.Show;
FrmLogin.Close;
end
else if Query.FieldByName('Class').AsInteger = 1 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

Ypin.Show ;
FrmLogin.Close;
end
else if Query.FieldByName('Class').AsInteger = 2 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

KcManager.Show;
FrmLogin.Close;
end
else if Query.FieldByName('Class').AsInteger = 3 then
begin
UserPwd.Text :='';
Query.Close;
Query.Free;

YpOut.Show;
FrmLogin.Close;
end
else
begin
UserPwd.Text :='';
Query.Close;
Query.Free;
FrmLogin.Close;
end;
except
MessageDlg('数据库连接出错!!!',mtERROR,[mbOK],0);

end;

end;
//最好释放的代码写在finally块
 
你程序里面有两句 : Query.Connection := Dm.ADOConnection1;
究竟是哪一句出错?
如果在 FormCreate 过程里面出错,那很可能就是 DM 还没有被 Created ,调整 DM 的创建顺序在 TFrmLogin 的前面。
 
是后面一句出错!
 
liyinwei说得对,查一下你创建顺序,
 
我在TFrmLogin前建了也是不行,一样在后面 Query.Connection := Dm.ADOConnection1;
这一句出错
 
你们帮我看看是不是Query :TADOQuery这个定义上的问题啊 ?
 
exit???[?][?]
 
楼主帖工程单元的代码出来看看。
 
program yiyao;

uses
Forms,
UMain in 'UMain.pas' {FrmMain},
Udb in 'Udb.pas' {DM: TDataModule},
ULogin in 'ULogin.pas' {FrmLogin},
UGuest in 'UGuest.pas' {FrmGuest};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TFrmMain, FrmMain);
Application.CreateForm(TDM, DM);
Application.CreateForm(TFrmLogin, FrmLogin);
Application.CreateForm(TFrmGuest, FrmGuest);
Application.Run;
end.
 
工程单元没有问题。
FrmLogin.Show 这一行代码是在哪里的?
TfrmMain.FormCreate 过程做了些什么?楼主贴出来看看。
 
FrmLogin.Show这个我没写上,原来是有的,这个没问题的.
TfrmMain.FromCreate这个我只写了让FrmLogin显示出来的代码而已
就是FrmMain的窗体事件
代码如下: if FrmLogin = nil then
FrmLogin := TFrmLogin.Create(nil);
FrmLogin.ShowModule
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
459
import
I
I
回复
0
查看
526
import
I
I
回复
0
查看
578
import
I
后退
顶部