各位朋友,小弟在多个窗体里面都有类似下面的代码,请问有没有朋友能帮忙将以下的代码做成一个过程,然后用过程的方式来调用,可以不用每一个窗体都写上一个类似下面的过程来UPDATE表,诚心请各位大虾赐教,谢谢!!!
  with Ado_Master_Frp  do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
    begin
      close;
      sql.Clear;
      sql.Text := 'update SO_1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text);
      parameters.ParamByName('Type_of_sample').Value := trim(ComboBox1.Text);
      parameters.ParamByName('topeople').Value := trim(ComboBox2.Text);
      parameters.ParamByName('Brand_name').Value := trim(ComboBox3.Text);
      parameters.ParamByName('Yarn_quality').Value := trim(ComboBox4.Text);
      parameters.ParamByName('Yarn_quality2').Value := trim(ComboBox5.Text);
      ExecSQL;
    end;
      
  with Ado_Query do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
    begin
      close;
      sql.Clear;
      sql.Text := 'update 表2 set aa=:aa, helo=:helo' + QuotedStr(edit1.Text);
      parameters.ParamByName(aa').Value := trim(ComboBox1.Text);
      parameters.ParamByName('helo').Value := trim(ComboBox2.Text);
      ExecSQL;
    end;

解决方案 »

  1.   


    procedure BuildReport(typepeople,topeople,brandname,quality1,quality2,str1,str2:string);
    Var
      tempquery: TADOQuery;
    Begin
      tempquery := TADOQuery.Create(nil);  try
        With tempquery Do
        Begin
          close;
          sql.Clear;
          sql.Text := 'update SO_1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(str1);
          parameters.ParamByName('Type_of_sample').Value := trim(typepeople);
          parameters.ParamByName('topeople').Value := trim(topeople);
          parameters.ParamByName('Brand_name').Value := trim(brandname);
          parameters.ParamByName('Yarn_quality').Value := trim(quality1);
          parameters.ParamByName('Yarn_quality2').Value := trim(quality2);
          ExecSQL;      close;
          sql.Clear;
          sql.Text := 'update 表2 set aa=:aa, helo=:helo' + QuotedStr(str2);
          parameters.ParamByName('aa').Value := trim(typepeople);
          parameters.ParamByName('helo').Value := trim(topeople);
          ExecSQL;
        End;
      Finally
        tempquery.Free;
      End;
    End;
      

  2.   

    楼上的朋友,首先衷心感谢你的回复,但可能你不是不明白,上面的代码,每一个窗体都会有,而且UPDATE的表都是不同,而且UPDATE的字段值都会不同,而且每一次数量都不同的,这个才是问题的难点所在,希望有朋友再次赐教,谢谢
      

  3.   

    建一个窗体基类,每个窗体都是由这个基类继承下来的, 这个基类里定义了你的程序可以抽象出共同特征的东西
    例如:在基类中定义 procedure LogData(value: string); //用来记录操作日志 还有常规定义为
    procedure ExecSql(aSQL: string);
    var
      lQuery: TAdoQuery;
    begin
      lQuery := TAdoquery.create(nil);
      try
        lQueyr.SQL.Text := aSQL;
        lQuery.Execute;
      finally
        lQuery.Free;
      end;
    end;
    由于只是执行更新语句并不返回结果集, 这样就好了, 还可以定义成函数用来返回是否成功等.
    调用时只需传入执行的SQL语句即可
    procedure Update;
    var
      lSQL: string;
    begin
      lSQL := 'update SO_1 set ' +
              ' Type_of_sample = ' + ComboBox1.Text +
              ',topeople = ' + ComboBox2.Text +
              ',Brand_name = ' + ComboBox3.Text +
                 .....
              ' Where 你的条件 ';
      ExecSQL(lSQL);
    end;
    这样看上去是不是好一点了, 另外为了以后维护起见,控件的命名最好应该起的有意义点
      

  4.   

    如果很多个窗体都要用,只能把SQL当参数传递了,貌似没有太大意义,要不做成存储过程吧
      

  5.   

    首先很衷心感谢楼上的几位大虾的热心回复,该贴子不是那么简单的,有着非常大的重要意义,因为可以减少代码的输入量,我将几个过程整理了一下,请看下面的几个过程,自己本来是有思路,但就是不懂实现,小弟先将自己的思路先跟几位大虾说一下,下面几个窗体的过程,都有几个共通点1、首先就是都通过TAdoquery来调用的,那么,这个复杂的过程当中就要设定一个Tadoquery的变量,方便用户传入当前窗体的Tadoquery
    2、因为Update的每个表都不同,所以就要设置一个变量:Table_Name,主要是给用户传入一个表名
    3、因为Where的每个条件都不同,所以就要设置一个变量:Where_Value,主要是用来给用户传入Where的条件值。
    4、Sql语句里面,每一次Update的字段的数量不同,有的时候是一个,有的时候是两个,有的时候是十几个,所以自己觉
    得要设置一个动态的数组来完成该动作,但具体的实现方便不懂实现,只有思路
    5、因为每次传入的Update的字段数量不同,所以对应的parameters.ParamByName('字段').value也应该有很多句,自己的思路是,可以做一个循环
       for i:=0 to 用户传入的数量 do
    parameters.ParamByName(动态数组[i]]).value:=用户传入的字段然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了:
    1、Update的只有一个字段,那么就写成
    Update过程名(当前窗体的adoquery,字段1,Where的条件值,要Update的表名,要循环1次);
    2、Update的只有两个字段,那么就写成
    Update过程名(当前窗体的adoquery,字段1,字段1,Where的条件值,要Update的表名,要循环2次);
    3、Update的N个字段,那么就写成
    Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,Where的条件值,要Update的表名,要循环N次);  with Adoquery do
        begin
          close;
          sql.Clear;
          sql.Text := 'update Table1 set dye_id=:dye_id where name=' + QuotedStr(edit1.text);
          parameters.ParamByName('dye_id').Value := trim(edit2.text);
          ExecSQL;
        end;
      with Ado_Master_Frp do
        begin
          close;
          sql.Clear;
          sql.Text := 'update SO_Sample_Order1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name where Sample_order_no=' + QuotedStr(DBEditEh3.Text);
          parameters.ParamByName('Type_of_sample').Value := trim(ComboBox1.Text);
          parameters.ParamByName('topeople').Value := trim(ComboBox2.Text);
          parameters.ParamByName('Brand_name').Value := trim(ComboBox3.Text);
          ExecSQL;
        end;
      with ado_tmp do 
        begin
          close;
          sql.Clear;
          sql.Text := 'update so_dkcbd1 set to_people=:to_people, rc=:rc, ZHI=:ZHI, sbzl=:sbzl, client=:client,' +
            'jbrc=:jbrc,zbr=:zbr,yy=:yy where dh=' + QuotedStr(trim(dh));
          parameters.ParamByName('to_people').Value := trim(ComboBox1.Text);
          parameters.ParamByName('rc').Value := trim(rc);
          parameters.ParamByName('ZHI').Value := trim(ComboBox2.Text);
          parameters.ParamByName('sbzl').Value := trim(ComboBox3.Text);
          parameters.ParamByName('client').Value := trim(ComboBox4.Text);
          Parameters.ParamByName('jbrc').Value := trim(jbrc);
          Parameters.ParamByName('zbr').Value := trim(ComboBox5.Text);
          Parameters.ParamByName('yy').Value := trim(ComboBox6.Text);
          ExecSQL;
        end;
      

  6.   

    补充一下,写少了点东西,呵呵!
    然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了: 
    1、Update的只有一个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,新值1,Where的条件值,要Update的表名,要循环1次); 
    2、Update的只有两个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,字段2,新值1,新值2,Where的条件值,要Update的表名,要循环2次); 
    3、Update的N个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,新值1,新值2,新值N,Where的条件值,要Update的表名,要循环N次); 期待论坛上的各位朋友都能看一下,看看这样复杂的过程有没有办法能做得出来,谢谢了!
      

  7.   

    楼主可能对语言不熟,过程可以传递数组参数,而且是可以复杂的数组参数,例如:先定义一对健值Record  TFieldValue = record
        Name: string;
        Value: Variant;
      end;定义生成该Record的方法
      function NewFieldValue(Name: string; Value: Variant);
      begin
        Result.Name := Name;
        Result.Value := Value;
      end;定义你的功能方法
      procedure TheProc(UpdateFields: array of TFieldValue;OtherParam...)
      begin
        ..循环UpdateFields...
      end;调用
      TheProc([NewFieldValue('Field1', fieldvalue1),
        NewFieldValue('Field2', fieldvalue2),
        ...], OthterParamValue...);  
      

  8.   

    如果采用循環UPDATE的方式,代碼是少了
    但是會增加程式執行的負擔
    然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了: 
    1、Update的只有一个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,Where的条件值,要Update的表名,要循环1次); 
    2、Update的只有两个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,字段1,Where的条件值,要Update的表名,要循环2次); 
    3、Update的N个字段,那么就写成 
    Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,Where的条件值,要Update的表名,要循环N次); 
    為什么不先循環,然后依次UPDATE呢?
      

  9.   

    //先定义一对健值Record
    TFieldValue = record
      Name: string;
      Value: Variant;
    end;//定义生成该Record的方法
    function NewFieldValue(Name: string; Value: Variant): TFieldValue;
    begin
      Result.Name := Name;
      Result.Value := Value;
    end;procedure MyCommProc(MasterTable, QueryTable: string; MasterFields, QueryFields: array of TFieldValue);
    var
      i: Integer;
    begin
       with Ado_Master_Frp  do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
        begin
          close;
          sql.Clear;
          sql.Text := 'update ' + MasterTable + ' set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text);
          for i := 0 to High(MasterFields) do
            parameters.ParamByName(MasterFields[i].Name).Value := trim(MasterFields[i].Value);
          ExecSQL; 
        end; 
          
      with Ado_Query do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
        begin 
          close; 
          sql.Clear;
          sql.Text := 'update ' + QueryTable + ' set aa=:aa, helo=:helo' + QuotedStr(edit1.Text);
          for i := 0 to High(MasterFields) do
            parameters.ParamByName(MasterFields[i].Name).Value := trim(MasterFields[i].Value);
          ExecSQL; 
        end;
    end;
      

  10.   

    楼上的大虾,首先非常感谢,但你的代码始终不是小弟想要的代码,首先sql.Text := 'update ' + MasterTable + ' set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text); 这一句就已经没有动态,因为不同的窗体,UPDATE的字段根本不可能是一样的,就是说,上面的SET之后的语句,第一个窗体SET的这段值都是不同的,再顶一下,希望大虾能再帮帮忙,谢谢了
      

  11.   


    我第一次回复给的是一种思路,第二次回复怕你不理解加的一些代码片断,剩下的就是拼Sql串的问题,以为你明白了。或者是我根本理解错你的问题了。