DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > 经验分享 > Delphi

Delphiadoquery处理多条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;

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号