if not ADOConnection1.InTransaction then
ADOConnection.BeginTrans;//打开事务
adoquery.insert;//这里可以反反复复的增加数据
adoquery.filedbyname().as**:=**;
adoquery.post;//此时的数据只是被提交到了数据集
ADOQuery1.LoadFromFile('C:\ClubID.lm');只将数据保存到结果集
将结果集的数据保存到数据库,你可以再建一个TABLE之类的
然后循环插入就行了
以前好象是这么写的,没试过什么特别好的方法,但可以实现
var
i:integer;
adoquery.first;
while adoquery.eof do begin
with adotable do
begin
Append;
for i:=0 to adoquery.FieldCount-1 do
fields[i].value:=adoquery.fields[i].value;
post;
end;
next;
end;
{ 说明:
建一个测试表Test1,
字段 类型 主键
id int PK
name char(20)
建立一个TADOConnection对象名为ADOCnn;
属性设置如下
ADOCnn.Mode := cmWrite; //只写
ADOCnn.ADOCnn.CursorLocation := clUseClient; //客户端游标
}
procedure TForm1.BatchTest;
var
intId, I: integer;
begin
with TADOQuery.Create(nil) do
try
Connection := ADOCnn;
LockType := ltBatchOptimistic; //批量更新
CursorType := ctOpenForwardOnly; //前向游标
SQL.Text := 'select id,name from test1';
Open;
intId := GetNextId; //取得下一个编号
if not ADOCnn.InTransaction then
ADOCnn.BeginTrans;
try
//测试插入数据
for I := intId to intId + 10000 do
AppendRecord([I,'ququ']);
//批量更新,有几个参数,查查帮助
UpdateBatch();
ADOCnn.CommitTrans;
except
on E: Exception do
begin
CancelBatch(); //取消更新
ADOCnn.RollbackTrans;
if Application.MessageBox('批量导入失败,是否查看详细信息?',
'失败', MB_YESNO + MB_ICONERROR) = IDYES then
Application.MessageBox(PChar(E.Message), '详细错误信息', MB_OK + MB_ICONERROR);
exit;
end;
end;
finally
Free;
end;
end;
//
function TForm1.GetNextId: integer;
begin
with TADOQuery.Create(nil) do
try
Connection := ADOCnn;
SQL.Text := 'select max(id) maxid from test1';
Open;
if IsEmpty then
Result := 0
else
Result := FieldByName('maxid').AsInteger;
Inc(Result);
finally
Free;
end;
end;