Delphi AdoQuery如何一次提交多条数据
3个回答
展开全部
是同一个数据库的话建议 写后台存储过程 delphi前台调用 可以用adoquery
或者adostoreproc ;如果是不同数据库需要用到两个adoquery,一个select通过调用open方法,一个insert 调用execsql方法,看例子从dbf文件到oracle数据库:function DbfToOracle(filename:string;orasid:string;uid:string;pass:string):Boolean;stdcall;
var
i:Integer;
tablename:string;
conora:TADOConnection;
aquerydbf:Tadoquery;
aqueryora:TADOQuery;
begin
if not FileExists(filename) then
begin
MessageBox(0,'Dbf File Not Found!','Error',MB_OK);
Result := False;
exit;
end
else
tablename := ExtractFileName(ChangeFileExt(filename,''));
conora := TADOConnection.Create(nil);
aquerydbf := Tadoquery.create(nil);
aqueryora := Tadoquery.create(nil);
try
aquerydbf.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="sourcetype=dbf;driver='
+'{Microsoft Visual Foxpro Driver};sourcedb='+extractfilepath(filename)+';"';
aquerydbf.SQL.Clear;
aquerydbf.SQL.Add('select * from '+tablename);
aquerydbf.Open;
aquerydbf.First; tablename := 'T'+tablename; //表名加了个T字符,重新起得名 conora.ConnectionString := 'Provider=OraOLEDB.Oracle.1;Password='+pass+';Persist Security Info=True;User ID='
+uid+';Data Source='+orasid+';Extended Properties=''PLSQLRSet=1''';
conora.LoginPrompt := False;
conora.Open;
aqueryora.Connection := conora;
aqueryora.SQL.Clear;
aqueryora.SQL.Add('select * from all_tables where owner=upper('''+uid+''') and table_name=upper('''+tablename+''')');
aqueryora.Open;
if aqueryora.RecordCount=0 then
begin
aqueryora.SQL.Clear;
aqueryora.SQL.Add('call sp_init_enviroment('''+uid+''')');
aqueryora.ExecSQL;
end;
aqueryora.SQL.Clear;
aqueryora.SQL.Add('select * from '+tablename);
aqueryora.Open;
conora.BeginTrans; while not aquerydbf.Eof do
begin
aqueryora.Insert;
for i := 0 to aquerydbf.FieldCount-1 do
begin
aqueryora.Fields[i].Value := aquerydbf.Fields[i].Value;
end;
aqueryora.Post;
aquerydbf.Next;
end;
try
conora.CommitTrans; except on e:Exception do
conora.RollbackTrans; end;
except on e:Exception do
begin
MessageBox(0,PAnsiChar(e.Message),'Open Database Error!',MB_OK);
aquerydbf.Free;
aqueryora.Free;
conora.Free;
Result:=False;
Exit;
end;
end; aquerydbf.Free;
aqueryora.Free;
conora.Free;
Result:=True;
end;
或者adostoreproc ;如果是不同数据库需要用到两个adoquery,一个select通过调用open方法,一个insert 调用execsql方法,看例子从dbf文件到oracle数据库:function DbfToOracle(filename:string;orasid:string;uid:string;pass:string):Boolean;stdcall;
var
i:Integer;
tablename:string;
conora:TADOConnection;
aquerydbf:Tadoquery;
aqueryora:TADOQuery;
begin
if not FileExists(filename) then
begin
MessageBox(0,'Dbf File Not Found!','Error',MB_OK);
Result := False;
exit;
end
else
tablename := ExtractFileName(ChangeFileExt(filename,''));
conora := TADOConnection.Create(nil);
aquerydbf := Tadoquery.create(nil);
aqueryora := Tadoquery.create(nil);
try
aquerydbf.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="sourcetype=dbf;driver='
+'{Microsoft Visual Foxpro Driver};sourcedb='+extractfilepath(filename)+';"';
aquerydbf.SQL.Clear;
aquerydbf.SQL.Add('select * from '+tablename);
aquerydbf.Open;
aquerydbf.First; tablename := 'T'+tablename; //表名加了个T字符,重新起得名 conora.ConnectionString := 'Provider=OraOLEDB.Oracle.1;Password='+pass+';Persist Security Info=True;User ID='
+uid+';Data Source='+orasid+';Extended Properties=''PLSQLRSet=1''';
conora.LoginPrompt := False;
conora.Open;
aqueryora.Connection := conora;
aqueryora.SQL.Clear;
aqueryora.SQL.Add('select * from all_tables where owner=upper('''+uid+''') and table_name=upper('''+tablename+''')');
aqueryora.Open;
if aqueryora.RecordCount=0 then
begin
aqueryora.SQL.Clear;
aqueryora.SQL.Add('call sp_init_enviroment('''+uid+''')');
aqueryora.ExecSQL;
end;
aqueryora.SQL.Clear;
aqueryora.SQL.Add('select * from '+tablename);
aqueryora.Open;
conora.BeginTrans; while not aquerydbf.Eof do
begin
aqueryora.Insert;
for i := 0 to aquerydbf.FieldCount-1 do
begin
aqueryora.Fields[i].Value := aquerydbf.Fields[i].Value;
end;
aqueryora.Post;
aquerydbf.Next;
end;
try
conora.CommitTrans; except on e:Exception do
conora.RollbackTrans; end;
except on e:Exception do
begin
MessageBox(0,PAnsiChar(e.Message),'Open Database Error!',MB_OK);
aquerydbf.Free;
aqueryora.Free;
conora.Free;
Result:=False;
Exit;
end;
end; aquerydbf.Free;
aqueryora.Free;
conora.Free;
Result:=True;
end;
展开全部
ADOQuery与数据库是不断开连接的,所以你只需要将更改写到ADOQuery里面,然后POST就到了数据库了,但是之前最好根据情况判定下是否需要开启事务
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
AdoQuery 不返回 数据吧 如果是数据集可以用 UpdateBatch() 批量提交
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询