Delphi-将参数传递给ADOquery

Ama*_*nda 5 delphi ado parameter-passing

亲爱的专家,我正在尝试过滤dbgrid连接到adoquery的结果,取决于用户选择4个复选框,用户可以选择一个或多个文件来相应地过滤数据我有这个代码,我不知道如何如果用户选择两个或更多复选框,则传递"和/或不传递"它.

Vw_Activity.SQL.Text:='select * from Vw_Activity where ';
if CBEmployee.Checked then
begin
Vw_Activity.SQL.Add('Emp_Name_Ar=:x');
Vw_Activity.Parameters.ParamByName('x').Value:=emp Name.Text;
end;


if CBTask.Checked then
begin
Vw_Activity.SQL.Add('Category_Name=:y');
Vw_Activity.Parameters.ParamByName('y').Value:=Pro blemCat.Text;
end;

if CBIncharge.Checked then
begin
Vw_Activity.SQL.Add('Support_name_En=:h');
Vw_Activity.Parameters.ParamByName('h').Value:=Sup portstaff.Text;
end;


if CBstatus.Checked then
begin
Vw_Activity.SQL.Add('Request_Status=:k');
Vw_Activity.Parameters.ParamByName('k').Value:=Req uestStatus.Text;
end;

Vw_Activity.Active:=true;
Run Code Online (Sandbox Code Playgroud)

等你的帮助

RRU*_*RUZ 5

你可以重写你的sql句子(检查最后的1 = 1)

select * from Vw_Activity where 1=1
Run Code Online (Sandbox Code Playgroud)

然后添加这样的每个条件

Vw_Activity.SQL.Text:='select * from Vw_Activity where 1=1 ';
if CBEmployee.Checked then
begin
  Vw_Activity.SQL.Add('AND Emp_Name_Ar=:x');
  Vw_Activity.Parameters.ParamByName('x').Value:=emp Name.Text;
end;


if CBTask.Checked then
begin
  Vw_Activity.SQL.Add('AND Category_Name=:y');
  Vw_Activity.Parameters.ParamByName('y').Value:=Pro blemCat.Text;
end;

if CBIncharge.Checked then
begin
  Vw_Activity.SQL.Add('AND Support_name_En=:h');
  Vw_Activity.Parameters.ParamByName('h').Value:=Sup portstaff.Text;
end;


if CBstatus.Checked then
begin
  Vw_Activity.SQL.Add('AND Request_Status=:k');
  Vw_Activity.Parameters.ParamByName('k').Value:=Req uestStatus.Text;
end;

Vw_Activity.Active:=true;
Run Code Online (Sandbox Code Playgroud)