如何利用ADO控件将sqlserver中符合条件的数据集批量插入至dbf格式的表中

app2001 2002-04-07 08:04:36
正在做着的一个系统,因涉及到与用户原有系统的整合,需将营业数据定时传至用户指定的
dbf格式的表上,两个数据源不在同一台服务器上,程序做好后,是需要二十四小时不停机使
用的,即一运行后,没有特殊原因,是不会关闭运行的,且不需要人工干预,自动运行的,
原用bde时,在access数据库中还能利用别名能将不同数据库的数据在一个query控件上用一条sql语句来完成批量插入,现在转换
成ado后,试了好多次就不行了,匆忙之中,只能建立两个连接,然后用循环来一条条插入,
实际使用后,感觉效率不高,请各位仁兄多多帮忙,怎样才能达到我的目的;
另:如有其它更好的方法也行,多谢了。
...全文
77 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
outer2000 2002-04-22
  • 打赏
  • 举报
回复
用TBATCHMOVE可以吧?
eiffeltower 2002-04-11
  • 打赏
  • 举报
回复
listen
app2001 2002-04-09
  • 打赏
  • 举报
回复
可我想要的是批量插入的方法呀,sql里有一个OPENROWSET()函数说是可支持
打开异构数据库,打开access的我晓得了,不知哪位仁兄能指点一二,如何打开vfp中dbf格式的数据库,
tanqth 2002-04-07
  • 打赏
  • 举报
回复
如果没错楼上的代码应该能行,
wylove 2002-04-07
  • 打赏
  • 举报
回复
下面的例子展示了从Foxpro 到SQL Server的数据转移方法。至于其他系统间的数据转移,只要根据目标系统的数据定义要求, 修改相应的Insert 语句。
程 序 代 码 如 下:
unit ConvertDBF;

interface

uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
StdCtrls, DBTables, Db, Grids, DBGrids;

type
TfrmConvertDB = class(TForm)
btnOK: TButton;
Label1: TLabel;
db1: TDatabase; {用于连接老数据库系统}
db2: TDatabase; {用于连接新数据库系统}
dbg: TDBGrid;
tblSource: TTable; {dbg的Datasource}
qryInsert: TQuery;
{用于存放生成的SQL Insert语句}
srcSource: TDataSource;
tblDest: TTable; {DBGrid1的Datasource}
DBGrid1: TDBGrid;
srcDest: TDataSource;
edFromtbl: TEdit;
Label2: TLabel;
Label3: TLabel;
edToTbl: TEdit;
procedure btnOKClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmConvertDB: TfrmConvertDB;

implementation

{$R *.DFM}
procedure TfrmConvertDB.btnOKClick
(Sender: TObject);
var iField :integer;
begin
if ((edTotbl.text<>'') and
(edFromtbl.text<>''))then begin
tblSource.TableName:=edFromtbl.text;
{指定TableName}
tblDest.TableName:=edTotbl.text;
with tblSource do begin
Open; {打开老系统的表}
while EOF=FALSE do begin
{逐条记录处理}
qryInsert.SQL.Clear;
qryInsert.sql.Add
('Insert into '+edTotbl.text + '(');
for iField:=0 to dbg.FieldCount-1 do begin
qryInsert.sql.add
(dbg.Fields[iField].DisplayLabel);
if iField<>dbg.FieldCount-1 then
qryInsert.sql.add(',');
end;
qryInsert.sql.add(') values(');
for iField:=0 to dbg.FieldCount-1 do begin
{进行数据类型转换}
if dbg.fields[iField].DataType=ftInteger then
qryInsert.sql.add(inttostr
(dbg.fields[iField].asInteger));
if dbg.fields[iField].DataType=ftFloat then
qryInsert.sql.add(floattostr
(dbg.fields[iField].asFloat));
if dbg.fields[iField].DataType=ftDate then
qryInsert.sql.add(''''+datetostr
(dbg.fields[iField].asDateTime)+'''');
if dbg.fields[iField].DataType=ftString then begin
if dbg.fields[iField].asString<>'' then
qryInsert.sql.add(''''+dbg.fields
[iField].asString+'''')
else
qryInsert.sql.add('NULL');
end;
if iField<>dbg.FieldCount-1
then qryInsert.sql.add(',');
end;
qryInsert.sql.add(')');
qryInsert.ExecSQL;
{把数据插入到新系统的表中}
next;
end;
end;
tblDest.Close;
tblDest.Open;;
ShowMessage(' 转换完毕! ');
end
else
ShowMessage
('请输入要插入数据的表的名称 ');
end;
end.
不知道对你是否有帮助?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧