比如在一个窗体里有一个“取消”和“确定”按钮,“取消”按钮批次取消所有修改,“确定”按钮批次提交:
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;