SQL 中的参数 - Delphi 7 [英] Parameters in SQL - Delphi 7
本文介绍了SQL 中的参数 - Delphi 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是 Delphi 7 和 Access 2007.
I am using Delphi 7 and Access 2007.
我想知道谁能告诉我如何将参数与 SQL 语句和 ADO 一起使用.
I want to know can anyone show me how to use Parameters with SQL statements and ADO.
什么是必要的编码等等.抱歉,我是 Delphi 的新手.
What is the necessary coding and so forth. Sorry I am new to Delphi .
推荐答案
只需设置查询的SQL
,然后填充参数.当然,使用对您有意义的参数名称;我刚刚使用了 LastName
和 FirstName
作为示例.在您编辑问题后,我已更新为使用 TADOQuery
而不是 TQuery
.
Simply set the query's SQL
, and then populate the parameters. Use parameter names that make sense to you, of course; I've just used LastName
and FirstName
for examples. I've updated to use TADOQuery
instead of just TQuery
after your edit to the question.
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT * FROM MyTable');
ADOQuery1.SQL.Add('WHERE LastName = :LastName AND');
ADOQuery1.SQL.Add('FirstName = :FirstName');
// Populate the parameters and open it
ADOQuery1.Parameters.ParamByName('LastName').Value := 'Jones';
ADOQuery1.Parameters.ParamByName('FirstName').Value := 'James';
ADOQuery1.Open;
// Use query results
ADOQuery1.Close;
// Populate parameters with new values and open again
// Populate the parameters and open it
ADOQuery1.Parameters.ParamByName('LastName').Value := 'Smith';
ADOQuery1.Parameters.ParamByName('FirstName').Value := 'Sam';
ADOQuery1.Open;
// Use new query results
ADOQuery1.Close;
这篇关于SQL 中的参数 - Delphi 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文