这都是很基本的东西呀。方法很多,如用Pos找逗号的位置然后取子串,把取过部分去掉再循环。一直Readln就能把文件读完。

解决方案 »

  1.   

    我倒是觉得,使用文本另存的方式比较简单。虽然方法笨拙了些,但是操作很简单。方法如下:
    1、先将文本(*.TXT)文件保存为CVS文件。
       原因是,CVS文件可以分列显示数据,它以逗号为标志。
    2、使用处理EXCEL文件对数据库写入的方法操作数据。
      

  2.   

    刚才有个错误,误将CSV写成CVS了。^_^ 
      

  3.   

    可以用SQL SERVER 的数据导入(ALL TASK\IMPORT)功能,在Source下拉列表中 选择TEXT文件,.....没问题的,do it!!
      

  4.   

    但是我的txt文件有1000多个呀,数据导入不是要一个一个选择导入吗?那不累晕?……
      

  5.   

    那就只好用读文件的方法了,其实我觉得代码量也不会太大,大概思路如下,供参考:1、遍历TXT文件存储的目录
    2、逐一处理文件:读一行文本 ——> 按照分隔符(比如“,”)逐项分解 ——> 将每行文本根据每一项存入一个字符串数组(或TStringList)
    3、处理完一个文件,写一次数据库。
      

  6.   

    另外,提取两个逗号之间的字符串,可以用一个双重循环语句来实现:
    首先遍历每行文本,将每个逗号在文本中的位置保存在一个数组中,然后用双重循环语句提取两个逗号之间的字符串,算了,我来给你写一下吧!(没有调试,你来试试吧)//注意,下面代码只处理一行文本
    procedure DivString(StrIn:string;var ResStrLst:TStringList);//结果在ResStrLst中
    var
      i,j,iLen:Integer;
      str:string; 
      lsCommaPos:TStringList;//save the positions of commas
    begin
      if TRIM(StrIn='') then Exit;  //为了便于操作,在每行文本的开头和末尾加上一个逗号
      StrIn := ',' + StrIn + ',';
      iLen := Length(StrIn);//length of StrIn
      
      lsCommaPos := TStringList.Create;
      try
        for i:=1 to iLen do 
          if (StrIn[i] =',' ) then lsCommaPos.Add(IntToStr(i)); 
        
        for i:=0 to lsCommaPos.Count -1 do
          for j:=1 to lsCommaPos.Count -1 do
          begin
            str := Copy(StrIn,StrToInt(lsCommaPos[i]),StrToInt(lsCommaPos[j]) - StrToInt(lsCommaPos[i]) -1); //两个逗号之间的字符串
            ResStrLst.Add(str); //好了,每行文本已经被分割成一个个字符串了
          end;     
      finally
        lsCommaPos.Free;
      end;
    end;//调用该过程
    procedure .......
    var
      lsContents:TStringList;
      StrRowText:string;//一行文本
    begin
      lsContents := TStringList.Create;
      
      try
        DivString(strRowText,lsContents); 
        ... //其它处理
      finally
        Contents.Free;  
      end; 
    end;
      

  7.   

      使用BCP。MS SQL 提供。
      bcp {[[database_name.][owner].]{table_name | view_name} | "query"}
        {in | out | queryout | format} data_file
        [-m max_errors] [-f format_file] [-e err_file]
        [-F first_row] [-L last_row] [-b batch_size]
        [-n] [-c] [-w] [-N] [-V (60 | 65 | 70)] [-6]
        [-q] [-C code_page] [-t field_term] [-r row_term]
        [-i input_file] [-o output_file] [-a packet_size]
        [-S server_name[\instance_name]] [-U login_id] [-P password]
        [-T] [-v] [-R] [-k] [-E] [-h "hint [,...n]"]EXPLAME:
       bcp mydatabase_name.mytable_name in myTextfile_name -e'errfile.txt' -c -t',' 
    -Sserver_Name  -Uuser_name -Ppassword 
       你作成一个.bat文件,包含所有的TXT文件及表的处理命令。执行.bat文件即可。修改。BAT文件即可改变操作。可编程生成。BAT文件。
       速度飞快哦! :-Q
     
      

  8.   

    昨天没来得及上网,后来自己搞定了,但是还要谢谢大家。特别是navy和smallBridge的建议。
    unit Unit1;interfaceuses
        Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
        StdCtrls, Grids, inifiles, Buttons, Db, DBTables, DBGrids;type
        TForm1 = class(TForm)
            OpenDialog1: TOpenDialog;
            Button3: TButton;
            Database1: TDatabase;
            DataSource1: TDataSource;
            ListBox1: TListBox;
        Query1: TQuery;
            procedure GetValue(stockname,Value: string);
            procedure Button3Click(Sender: TObject);
        private
        { Private declarations }
        public
        { Public declarations }
        end;var
        Form1: TForm1;implementation{$R *.DFM}
    procedure Tform1.GetValue(stockname,Value: string);
    var iPosion: integer;
         s1,s2,s3,s4,s5,s6:string;
    begin
        s1:=stockname;    iPosion := Pos(',', Value)-1;
        s2:=copy(value,0,iposion);    value:=copy(value,iposion+2,length(value)-iposion);
        iPosion := Pos(',', Value)-1;
        s3:=copy(value,0,iposion);    value:=copy(value,iposion+2,length(value)-iposion);
        iPosion := Pos(',', Value)-1;
        s4:=copy(value,0,iposion);    value:=copy(value,iposion+2,length(value)-iposion);
        iPosion := Pos(',', Value)-1;
        s5:=copy(value,0,iposion);    value:=copy(value,iposion+2,length(value)-iposion);
        iPosion := Pos(',', Value)-1;
        s6:=copy(value,0,iposion);//    if not table1.Active then table1.Open;
        {table1.append;
        table1.FieldByName('id').asstring:='0';
        table1.FieldByName('stockid').asstring:=s1;
        table1.FieldByName('curdate').asstring:=s2;
        table1.FieldByName('openprice').asstring:=s3;
        table1.FieldByName('highest').asstring:=s4;
        table1.FieldByName('lowest').asstring:=s5;
        table1.FieldByName('latest').asstring:=s6;
        table1.post;}
        with query1 do
        begin
         Close;
         query1.ParamByName('id').asstring:='0';
         query1.ParamByName('stockid').asstring:=s1;
         query1.ParamByName('curdate').asstring:=s2;
         query1.ParamByName('openprice').asstring:=s3;
         query1.ParamByName('highest').asstring:=s4;
         query1.ParamByName('lowest').asstring:=s5;
         query1.ParamByName('latest').asstring:=s6;
         ExecSQL;
       end;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
        I, J: integer;
        F: TextFile;
        FirstLine: string;
        openprice, hightest: string;
        tempstr: string;begin
        OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist];
        OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
        OpenDialog1.FilterIndex := 2; { start the dialog showing all files }
        if OpenDialog1.Execute then
            with OpenDialog1.Files do
                for I := 0 to Count - 1 do
                begin
                    listbox1.Items.LoadFromFile(Strings[I]);
                    tempstr := ExtractFilename(Strings[I]);
                    tempstr := copy(tempstr, 0, pos('.', tempstr) - 1);
                    for j := 1 to listbox1.Items.Count - 1 do
                        GetValue(tempstr,listbox1.Items[j]);
    //                table1.Close;
    //                table1.open;
                end;
    end;end.