delphi 能否把.txt文件的数据导入到Access数据库中  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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 from [excel 8.0;database=].[$]

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

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.038957118988037 seconds