• 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;
  • 相关阅读:
    设计模式之组合模式
    设计模式之桥接模式
    设计模式之装饰模式
    设计模式之代理模式
    总结的一些MySQL索引相关的知识点
    软件架构设计-五视图方法论
    博客迁移
    IMDB.COM排名算法(贝叶斯公式)和Reddit评论排行算法
    利用ratchet 和 ZeroMQ 实现即时(推送)聊天的功能
    composer Ratchet 实验心得
  • 原文地址:https://www.cnblogs.com/msony924840/p/4882272.html
Copyright © 2020-2023  润新知