我想把一个文本文件的数据导入到Access数据库中(字段及类型完全一致),能否做到?

解决方案 »

  1.   

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

  2.   

    我想将.txt文件放入一个已经存在的表中而不是在数据库中再建一个表,请问如何做?
      

  3.   

    可以,自己写个入库程序吧,txt的数据排列要有规律
      

  4.   

    我刚学使用delphi,不太会用,请问它的入库程序怎么写?
      

  5.   

    偶现在很穷,挣分结贴 偶现在很穷,挣分结贴 偶现在很穷,挣分结贴到论坛去看一看
    http://www.envanet.com
    我的论坛,这样说是不是有拉条皮的嫌疑
    不过我确实放了很多高程考试的资料在上面
    好的话,给点分支持一下
      

  6.   

    能否给点分
    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;
      

  7.   

    告诉你一个最快的方法,用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>$]