Delphi中的SQL查询中的用户定义参数 [英] user defined parameters in a SQL query in Delphi

查看:133
本文介绍了Delphi中的SQL查询中的用户定义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个功能,该功能将在允许用户登录之前验证用户.用户ID,密码和名称存储在数据库中(MS Access).该函数获取用户输入的ID,密码和名称,并将其与数据库中找到的ID,密码和名称进行比较.但是,当尝试通过SQL查询查找用户信息时,会发生以下错误:参数Brent没有默认值" Brent是用户通过从下拉菜单中选择它来输入的用户名(如避免出现拼写错误等.Delphi代码如下:


i am trying to create a function that will verify users before allowing them to login. The userID, Password and Name are stored in a data-base (MS Access). The function takes the ID, Password and Name that the user has entered and compares it to that found in the database. However when trying to find the users information through a SQL query the following error occurs: ''Parameter Brent has no default value'' Brent is the user name entered by the user by means of selecting it from a drop-down menu (as to avoid spelling errors etc. the Delphi code follows:


function check (Name, ID, Password : string) : string;
var
  myLoop : integer;
  myField : string;
begin
  form1.ADOQuery1.Active := false;
  form1.ADOQuery1.SQL.Text := ''SELECT [employee ID], Password, Name FROM Employees WHERE Name = '' + name;
  form1.ADOQuery1.Active := true;

  {for myLoop := 1 to 3 do
  begin
  myField := form1.DataSource1.DataSet.Fields[myLoop].AsString;
  showmessage(myField);
  unit1.arrEmployee[myLoop] := myField;
  end;

  if (arrEmployee[1] = ID) AND (arrEmployee[2] = Password) AND (arrEmployee[3] = Name) then
  showmessage(''all your information is correct'') else
  showmessage(''your information is not all correct'');  }  

推荐答案

发生错误的原因可能是因为您记录了主题的名称字段为Null,并在查询末尾添加了null检查:

The error occurred maybe because you have record(s)that the Name field of theme are Null , add and null check to end of your query :

SELECT [employee ID], Password, Name FROM Employees WHERE Name = ' + name + ' AND Name IS Not Null';



为什么您要用这种方式检查登录名?您可以轻松完成所有登录检查,例如:



Why you check the login this way ? you can easily do all login checks usign T-SQL query like :

form1.ADOQuery1.Active := False;
form1.ADOQuery1.SQL.Text := 'SELECT [employee ID], Password, Name FROM Employees                                                  WHERE Name = ' + name + ' AND Password = ' + Password + ' AND[employee ID] =' + ID;
form1.ADOQuery1.Active := True;

if (form1.ADOQuery1.RecordCount > 0) then
  // Login
else
  // Login failed


这篇关于Delphi中的SQL查询中的用户定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆