zoukankan      html  css  js  c++  java
  • Delphi ADOQuery处理多条SQL语句

    Delphi(Pascal) code
    var
      sqlStr:String;
    begin
      sqlStr:= ' begin '
      sqlStr:= sqlStr+ 'update table1 set col1 = ''test'' where 1=2;';
      sqlStr:= sqlStr+ 'update table1 set col1 = ''test2'' where 1=2;';
      sqlStr:= sqlStr+ ' end ';

      adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add(sqlStr);
      adoquery1.ExecSQL;
    end;

    把sql语句用begin...end包起來,再提交給DB处理,就OK了!

    ADOQuery的批处理方式
    比如在一个窗体里有一个“取消”和“确定”按钮,“取消”按钮批次取消所有修改,“确定”按钮批次提交:
    1.设置QryDef(数据集)的LockType为ltBatchOptimistic,CursorType为ctStatic,CursorLocation为clUseClient
    2.“确定”按钮的单击事件为:
      if QryDef.Connection.InTransaction then
      begin
        try
          QryDef.UpdateBatch();
          QryDef.Connection.CommitTrans;
        except
          QryDef.Connection.RollbackTrans;
        end;
      end;
    3.“取消”按钮的单击事件为:
      QryDef1.CancelBatch;
    4.初始插入数据:
      Qry.LoadFromFile(ExtractFilePath(Application.ExeName) + 'ClassifyDefine');
      {$IFDEF Test}codesite.SendMsg('3'); {$ENDIF}
      while not Qry.Eof do
      begin
        QryDef.Append;
        QryDef.FieldByName('name').AsString := Qry.FieldByName('name').AsString;
        if not Qry.FieldByName('Money1').IsNull then
       QryDef.FieldByName('Money1').AsCurrency := Qry.FieldByName('Money1').AsCurrency;
        if not Qry.FieldByName('Money2').IsNull then
       QryDef.FieldByName('Money2').AsCurrency := Qry.FieldByName('Money2').AsCurrency;
        QryDef.Post;
        Qry.Next;
      end;
      QryDef.UpdateBatch(); 

    5.批处理方式其它实例:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    adoconnection1.begintrans;
    try
    adoquery.close;
    adoquery.sql.clear;
    adoquery.sql.add(insert 语句);
    adoquery.execsql;
    如果还有insert 语句则:
    adoquery.close;
    adoquery.sql.clear;
    adoquery.sql.add(insert 语句);
    adoquery.execsql;直到所有insert 语句完成.
    adoconnection1.committrans;
    except
    adoconnection1.rollbacktrans;
    end;
    end;

    用adoquery取指定字段所有值问题
    我要在在DBComboBox里显示出来啊
    while   not   adoquery1.Eof   do  
      begin  
          ComboBox1.Items.Add(adoquery1.fieldbyname('id').asstring);   //id改为你要指定的字段  
          adoquery1.Next;  
      end;
    在DBComboBox里显示出来啊
    如果是DBComboBox  
      procedure   TForm1.FormCreate(Sender:   TObject);  
      begin  
          adoquery1.SQL.Add('select   *   from   test);  
          adoquery1.Open;  
          DBComboBox1.DataField:='id';  
          while   not   adoquery1.Eof   do  
          begin  
              DBComboBox1.Items.Add(adoquery1.fieldbyname(DBComboBox1.DataField).AsString);  
              adoquery1.Next;  
          end;  
    end;

    xxx.sql.text := 'insert into t_log3(name,czsj,czlog)values('''+a +''','''+ b+''','''+c+''')';

    xxx.sql.text := 'insert into t_log3(name,czsj,czlog)values(:a1,:b1,:c1)';
    xxx.parameters.parambyname('a1').values := a;
    xxx.parameters.parambyname('b1').values := b;
    xxx.parameters.parambyname('c1').values := c;

  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/2178735.html
Copyright © 2011-2022 走看看