delphi 能否把.txt文件的数据导入到Access数据库中


本文整理自网络,侵删。

 
在Form上放一个ADOConnection,连结指向目标Access库
比如txt文件在c:\temp\aaaa.txt
就执行
ADOConnection.Connected := True;
ADOConnection.Execute(’Select * Into abcd From [Text;Database=c:\temp].aaaa.txt’);


方法2:
procedure TForm1.btnimporttextClick(Sender: TObject);
var
 TextFileVar: Textfile ;
 fileN,s: string;
 testcode,name,school,grade,temp:string;
 num:integer;
begin
 num:=0;
 if opendialog1.execute then
 begin
   FileN:=opendialog1.FileName;
   If FileN='|' Then
     Exit;
   AssignFile ( TextFileVar , FileN ) ;
   Reset(TextFileVar);
   memo1.Lines.LoadFromFile(opendialog1.FileName);
   if messagedlg('确定要将该文本导入数据库吗?',mtconfirmation,[mbyes,mbno],0)=mryes then
   begin
   while not SeekEof(TextFileVar) do
   begin
     Readln ( TextFileVar , temp ) ;
     if pos('|',temp)>0 then
     begin
       testcode:=copy(temp, 1, pos('|',temp)-1);
       temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
       name:=copy(temp, 1, pos('|',temp)-1);
       temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
       school:=copy(temp, 1, pos('|',temp)-1);
       temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
       grade:=copy(temp, 1, pos('|',temp)-1);
       adoquery1.Close;
       adoquery1.SQL.Clear;
       adoquery1.SQL.Add('select testcode from pystudent where testcode='+testcode);
       adoquery1.Open;
       if adoquery1.RecordCount=0 then
         begin
          adotable1.Open;
          adotable1.insert;
          adotable1.fields[0].AsString:=testcode;
          adotable1.fields[1].AsString:=name;
          adotable1.fields[2].AsString:=school;
          adotable1.Fields[3].AsString:=grade;
          adotable1.post;
         end
         else
         num:=num+1;
         adoquery1.Close;
        s:=inttostr(num);
     end;
   end;
   showmessage('有'+s+'条记录相同而未导入数据库');
   closeFile(TextFileVar);
   adotable1.Close;
   showmessage('有效文件成功导入数据库');
  end;
 end;


方法3:
告诉你一个最快的方法,用SQLServer连接DBF
在SQLServer中执行
SELECT * into bmk
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
  'Data Source="e:\share";User ID=Admin;Password=;Extended properties=dBase 5.0')...bmk
这样就可以把e:\share中的bmk.dbf表导入到Sqlserver中,
速度是最快的
把压箱底的方法都告诉大家
在Form上放一个ADOConnection,连结指向目标Access库
比如txt文件在c:\temp\aaaa.txt
就执行
ADOConnection.Connected := True;
ADOConnection.Execute('Select * Into abcd From [Text;Database=c:\temp].aaaa.txt');
就一切Ok了,这个方法一定行的,我好不容易才研究出来的
有了这两个例子,异构数据库之间导数据的问题就举一反三,迎刃而解了。

把Excel导入到Access中,同txt类似
select * into <table name> from [excel 8.0;database=<filename>].[<sheetname>$]

来源:https://bbs.csdn.net/topics/30361870?locationNum=15&fps=1

相关阅读 >>

Delphi 获取网卡信息(支持多网卡)

Delphi 7 简单语法

Delphi firedac tfdconnection连接mysql数据库

Delphi superobject 序列数据集

Delphi 数据库重置用户密码制作

Delphi自带的indy控件实现md5加密

Delphi datasanp的控制老大-dsserver

Delphi 简化版的inputquery 中文版输入对话框

Delphi 调用js解决全角转半角

Delphi windows 根据进程名结束进程的代码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...