谁能帮我看看代码,到底是哪里出错了。不胜感激(20分)

  • 主题发起人 主题发起人 dreamblue
  • 开始时间 开始时间
D

dreamblue

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables;

type
TForm2 = class(TForm)
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Query1: TQuery;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
with query1 do
begin
close;
sql.clear;
sql.add('select yonghu,mima,qx from pasw.db where yonghu=:yonghu and mima=:mima and qx=:qx');
params[0].asstring:=combobox1.text;
params[1].asstring:=edit2.text;

open;
if query1.recordcount>0 then
begin
if params[2].asstring:='1' then
begin
form2.hide;
form1.show;
form1.n1.Enabled:=fasle;
end
else
begin
form2.hide;
form1.show;
end;
end
else
showmessage('您输入的用户名或密码错误');
combobox1.text:='';
edit2.text:='';
end;

注:以上代码的目地是根据权限判断登陆后的界面菜单是否显示。
数据库pas.db中
yonghu代表用户
mima代表密码
qx代表权限

 
qx为1时不显示form上的n1
否则显示n1
 
sql语中 =: 右边的yonghu,mima,qx是你定义的变量吧?应该将所赋的值用引号引起来,
即改为:
'select yonghu,mima,qx from pasw.db where yonghu=:'''+yonghu+''' and mima=:'''+mima
+''' and qx=:'+qx+''''

 
qx没有赋值呀
 
>>'select yonghu,mima,qx from pasw.db where yonghu=:yonghu and mima=:mima and qx=:qx');
联系上下文 我发现这个写的有点怪,要么是我没有看懂你的思路,
要么是你的程序写的不对了.
你把返回值也放在 "and qx=:qx"这里,这样有问题哦
Query.Open返回后利用FieldByName就可以访问结果了

 
if params[2].asstring:='1' then
:=是赋值啊。。
 
试试下面代码,我是假设combobox2中设置的是qx的值!
procedure TForm2.Button1Click(Sender: TObject);
var
ssql:string;
begin
ssql:='select yonghu,mima,qx from pasw.db where yonghu=' + combobox1.text + 'and mima='+ edit2.text + 'and qx='+ combobox2.text;
with query1 do
begin
close;
sql.clear;
sql.add (ssql);

open;
if query1.recordcount>0 then
begin
if query1.fieldbyname(qx).asstring ='1' then
begin
form2.hide;
form1.show;
form1.n1.Enabled:=fasle;
end
else
begin
form2.hide;
form1.show;
end;
end
else
showmessage('您输入的用户名或密码错误');
combobox1.clear;
edit2.clear;
end;

 
思路没什么错,但是程序肯定编译不过!
open;
if query1.recordcount>0 then
begin
if params[2].asstring:='1' then

             | 
判断语句应该用‘=’而不是‘:=’。‘:=’是赋值。
---------------------------------------------------------------- 
sql.add('select yonghu,mima,qx from pasw.db where yonghu=:yonghu and mima=:mima and qx=:qx');
params[0].asstring:=combobox1.text; ^
params[1].asstring:=edit2.text;                         如果QX没有赋值,那么底下的
                                     query1返回的值都是为空"if query1.recordcount>0 then"就一直不成立
             


 
真的谢谢大家这么热心,但我的思路是这样的,登陆界面有两个用户可以选择,一个是系统
管理员,一个是一般用户,对应的数据库里的权限是1和2,如果我以系统管理员的身份登陆
,则显示form1里的n1,否则不显示,qx是根据选择的用户定的,而不是登陆时选择的,请大
家帮我改改。
谢谢了。
 
wrong one:
you must set params[2].asstring:=? before open query!
wrong two:
"if params[2].asstring:='1' then" must be "if params[2].asstring='1' then"
^ ^
you shold be like this
...
with query1 do
begin
close;
sql.clear;
sql.add('select qx from pasw.db where yonghu=:yonghu and mima=:mima');
params[0].asstring:=combobox1.text;
params[1].asstring:=edit2.text;

open;
if query1.recordcount>0 then
begin
//if params[2].asstring:='1' then
if fieldbyname('qx').asstring= 1 then
begin
form2.hide;
form1.show;
form1.n1.Enabled:=fasle;
end
else
begin
form2.hide;
form1.show;
end;
end
else
showmessage('您输入的用户名或密码错误');
combobox1.text:='';
edit2.text:='';
end;

            



 
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables;

type
TForm2 = class(TForm)
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Query1: TQuery;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
with query1 do
begin
close;
sql.clear;
sql.add('select yonghu,mima,qx from pasw.db where yonghu=:yonghu and mima=:mima');
params[0].asstring:=combobox1.text;
params[1].asstring:=edit2.text;
open;
if Not isEmpty then
begin
First;
if Trim(FieldValues('qx'))='1' then
begin
form2.hide;
form1.show;
form1.n1.Enabled:=fasle;
end
else begin
form2.hide;
form1.show;
end;
end
else begin
showmessage('您输入的用户名或密码错误');
combobox1.text:='';
edit2.text:='';
end;
end;
 
改成:
sql.add('select yonghu,mima,qx from pasw.db where yonghu=:yonghu and mima=:mima');

你SQL里面的QX都没有赋值就Open数据库,怎么可以吖!!
干脆你删掉好了。

下面的就应该没有问题了。
 
最后少了个end;[:(]
 
谢谢大家,问题已经解决,
 
多人接受答案了。
 
后退
顶部