Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何实现:相同的查询但不同的连接_Delphi - Fatal编程技术网

Delphi 如何实现:相同的查询但不同的连接

Delphi 如何实现:相同的查询但不同的连接,delphi,Delphi,我有相同的查询,只有连接更改: if DataModule1.1_CONNECTION.Connected = true then begin DataModule1.ZAM_GESLO.SQL.Text:='select user,pwd from users where user = :a'; DataModule1.ZAM_GESLO.Params.ParamByName('a').AsString := DataModule1.LOGIN_QUERY.FieldByName('user'

我有相同的查询,只有连接更改:

if DataModule1.1_CONNECTION.Connected = true
then begin
DataModule1.ZAM_GESLO.SQL.Text:='select user,pwd from users where user = :a';
DataModule1.ZAM_GESLO.Params.ParamByName('a').AsString := DataModule1.LOGIN_QUERY.FieldByName('user').AsString;
DataModule1.ZAM_GESLO.Open;
cxGrid1.ActiveLevel.GridView := MYGRIDVIEW1;
end else 
if DataModule1.2_CONNECTION.Connected = true
then begin
DataModule1.ZAM_GESLO.SQL.Text:='select user,pwd from users where user = :a';
DataModule1.ZAM_GESLO.Params.ParamByName('a').AsString := DataModule1.LOGIN_QUERY.FieldByName('user').AsString;
DataModule1.ZAM_GESLO.Open;
cxGrid1.ActiveLevel.GridView := MYGRIDVIEW2;
end;
.......

这是一个很长的路要走,所以我想知道是否可以用任何其他优化的方式来完成,这样我就不必再次编写相同的查询了?

要保存重复的代码,您可以在表单中添加一个过程

procedure TxForm.UseDataSet(ADataSet : TxDataSet; AUserName : String);
begin
  if ADataSet.Active then
    ADataSet.Close;
  ADataSet.SQL.Text:='select user, pwd from users where user = :a';
  ADataSet.Params.ParamByName('a').AsString := AUser;
  ADataSet.Open;
  cxGrid1.ActiveLevel.GridView := AGridView;
end;
注意:我在ADataset参数的类型中使用了“x”,因为我不知道您使用的是哪种类型的数据集,同样也不知道表单的类型

然后,您可以将代码重新编写为

if DataModule1.1_CONNECTION.Connected then 
  begin
    UseDataSet(DataModule1.ZAM_GESLO,
      DataModule1.LOGIN_QUERY.FieldByName('user').AsString);
  end 
else 
  if DataModule1.2_CONNECTION.Connected then
    begin
      UseDataSet(DataModule1.ZAM_GESLO,
        DataModule1.LOGIN_QUERY.FieldByName('user').AsString);
    end;

顺便说一句,当你问你的问题时,很明显你想知道重复的代码。当您发现自己使用不同的对象编写基本相同的代码时,请考虑编写一个包含重复代码并在对象上操作为参数的过程(或函数)。< / P>不要询问是否为真,否则将不存在真正的ER。它已连接或未连接,如果DataModule1.1_CONNECTION.connected,则可以编写它
;如果DataModule1.1_CONNECTION.connected,则可以编写它
;如果DataModule1.1_CONNECTION.connected,则可以编写它
;如果DataModule1.1_CONNECTION.connected,则可以编写它