如何使用Delphi中的dbExpress将参数传递给查询 [英] How to pass a parameter to a query using dbExpress in Delphi
问题描述
我想使用dbExpress TSQLQuery组件。但是我不知道如何写SQL来添加一个参数。我会举一个例子,也许会更清楚我的问题是什么。
I want to use a dbExpress TSQLQuery component. But i do not know how to write the SQL to add a parameter. I will give an example maybe it will be more clear what my problem is.
在TADOQuery中,以下工作:
In a TADOQuery the following works:
SELECT*
FROM sometable
WHERE sometable.id = :value;
现在在上面的例子中,使用参数前的冒号(:)将参数传递给查询名称。但是当我尝试使用TSQLQuery进行此操作时,我收到以下错误:
Now in the above example you pass the parameter to the query using the colon (:) before the parameter name. But when I try and do that with the TSQLQuery, I get the following error:
dbExpress driver does not support the TDBXTypes.UNKNOWN data type. Vendor Error Message.
现在,如果这不是在TSQLQuery组件中传递参数的方式,有人可以协助我。这是我的新领域。
Now if this is not the way that you pass a parameter in a TSQLQuery component, can someone please assist me. This is new territory for me.
我使用Firebird数据库,Im使用Delphi XE2
Im using a Firebird database, and Im using Delphi XE2
推荐答案
要设置参数的属性,您必须使用 Params
属性。从这里,您可以使用索引或名称访问每个paramer,设置参数的值使用属性 AsString , AsInteger 等等,取消了类型领域的。
To set the properties of a parameter you must use the Params
property. From here you can access each paramer using a index or the name, to set the value of the parameter use one of the properties AsString, AsInteger an so on, depeding of the type of the field.
查看此示例
var
LSQLQuery : TSQLQuery;
begin
LSQLQuery:=TSQLQuery.Create(nil);
try
LSQLQuery.SQLConnection:=SQLConnection1;
LSQLQuery.CommandText:='Select FIRST_NAME from EMPLOYEE Where EMP_NO=:Param1';
LSQLQuery.Params.ParamByName('Param1').AsInteger:=2;//or using the index of the parameter LSQLQuery.Params[0].AsInteger:=2;
LSQLQuery.Open;//Execute the query
ShowMessage(LSQLQuery.FieldByName('FIRST_NAME').AsString); //get the data
LSQLQuery.Close;
finally
LSQLQuery.Free;
end;
end;
这篇关于如何使用Delphi中的dbExpress将参数传递给查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!