首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

登录数据库,该怎么解决

2013-01-07 
登录数据库想设计一个登录界面,用户输入正确的ID和password后提示‘欢迎’,输入有误时提示用户名或密码有误

登录数据库
想设计一个登录界面,用户输入正确的ID和password后提示‘欢迎’,输入有误时提示'用户名或密码有误'代码如下

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, DBCtrls, DB, ADODB;

type
  TForm1 = class(TForm)
    con1: TADOConnection;
    qry1: TADOQuery;
    ds1: TDataSource;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    btn1: TButton;
    btn2: TButton;
    edt1: TEdit;
    edt2: TEdit;
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
    qry1.Close;
    qry1.SQL.Clear ;
    qry1.SQL.Add('select ID,password from 信息表 where (edt1.text:=ID) and (edt2.text:=password)') ;
    qry1.Open;
    if (edt1.Text='ID') and (edt2.Text='password') then
     ShowMessage('Welcom!')
     else
     ShowMessage('ID or password is wrong!');

end;
procedure TForm1.btn2Click(Sender: TObject);
begin
 Close ;
end;

end.

运行后输入正确的id 和password 报错 [img=http://my.csdn.net/my/album/detail/1352267][/img]


[解决办法]
qry1.SQL.Add('select ID,password from 信息表 where (edt1.text:=ID) and (edt2.text:=password)') ;
  qry1.Open;
  if (edt1.Text=qry1.fieldbyname('ID').AsString) and (edt2.Text=qry1.fieldbyname('password').Asstring) then
  ShowMessage('Welcom!')
  else
  ShowMessage('ID or password is wrong!');
[解决办法]

procedure TForm1.btn1Click(Sender: TObject);
begin
  qry1.Close;
  qry1.SQL.Clear ;
  qry1.SQL.Add('select 1 from 信息表 where ID=:A and password=:B');
  qry1.Parameters.ParamByName('A').Value:=edt1.text;
  qry1.Parameters.ParamByName('B').Value:=edt2.text;
  qry1.Open;
  if not qry1.IsEmpty then
    ShowMessage('Welcom!')
  else
    ShowMessage('ID or password is wrong!');
end;

[解决办法]
引用:
还是同样的错误

你把整个程序都发出来吧。。
[解决办法]
 qry1.SQL.Add('select ID,password from 信息表 where ('+QuotedStr(edt1.text)+'=ID) and ('+QuotedStr(edt2.text)+'=password)') ;



qry1.Open;
  if qry1.RecountCount > 0 then
  ShowMessage('Welcom!')
  else
  ShowMessage('ID or password is wrong!');

热点排行