3

私はC#を使用しています。

オブジェクトにテキスト ファイルを取り込もうとしています。ODBC接続を使用していますが、次のようになります

Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\Users\Owner\Desktop\IR\IR_Files\Absolute;拡張子=asc,csv,tab,txt;

接続はできますが、列を分離できません。schema.ini ファイルを使用していますが、機能しません。これが私のスキーマファイルです。

[MyTextFile.CSV]
Format=Delimited(|)
ColNameHeader=False
Col1=fullstockn Text
col2=FULLINFO Text
MaxScanRows=0
CharacterSet=ANSI

テキストファイルはこんな感じ。

fullstockn|FULLINFO

"555555"|

内容 : Neuf Ttudes sur l ここにもう少しテキストがあります.....

4

5 に答える 5

4

次の接続文字列を使用します

string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text;HDR=YES;Format=Delimited(|)\";", Path.GetDirectoryName(path));

および、通常は Schema.ini ファイルで始まります

[myFile.txt]
Format=Delimited(|)
TextDelimiter="none"

そして、次の方法でリーダーを実行します

command.CommandText = String.Format("SELECT * FROM [{0}]", Path.GetFileName(path));
OleDbDataReader reader = command.ExecuteReader();

また、テキスト ファイル ドライバーに関するMSDN ページは、最初に調査したときに役に立ちました。具体的には、ファイルのページSchema.iniが非常に役立ちます。

于 2009-01-22T22:53:06.117 に答える
0

私はいつもこの種の操作のために自分でコードを書いています。これは、私がこの目的のために少し前に書いた抽象クラスの例です。必要に応じて、変更またはサブクラス化できます

public abstract class ReadTextFile : ILoadable
{
    public string Path { get; set; }
    public UploadFileType FileType { get; set; }
    protected internal List<List<string>> Table { get; set; }
    public Guid BatchID { get; set; }

    /// <summary>
    /// Method that loads the raw text into a collection of strings
    /// </summary>
    /// <returns></returns>
    public bool Load()
    {
        Table = new List<List<string>>();
        var splitter = Convert.ToChar("\t");
        try
        {
            using (TextReader tr = new StreamReader(Path))
            {
                // Discard the first line
                String line = tr.ReadLine();

                // Read and display lines from the file until the end of the file is reached.
                while ((line = tr.ReadLine()) != null)
                {
                    Table.Add(line.Split(splitter).ToList<string>());
                }
                tr.Close();
                tr.Dispose();
            }
            return true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }
    }
    public string Left(string param, int length)
    {
        //we start at 0 since we want to get the characters starting from the
        //left and with the specified lenght and assign it to a variable
        string result = param.Substring(0, length);
        //return the result of the operation
        return result;
    }
    public string Right(string param, int length)
    {
        //start at the index based on the lenght of the sting minus
        //the specified lenght and assign it a variable
        string result = param.Substring(param.Length - length, length);
        //return the result of the operation
        return result;
    }
}
于 2009-01-22T22:36:54.293 に答える
0

これに ODBC 接続を使用する必要がある理由はありますか? テキストファイルを直接開いて自分で解析する方が簡単だと思います。

于 2009-01-22T22:16:32.227 に答える
0

これが重要かどうかはわかりませんが...

dbq 属性の末尾の「\」が欠落している可能性があります...

編集:実際には...投稿したテキストには、2列ではなく3列があります...(1ではなく2パイプ)

于 2009-01-22T22:16:53.570 に答える
0

この接続文字列を使用してみてください

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Owner\Desktop\IR\IR_Files\Absolute\MyTextFile.CSV;Extended Properties='text'

と:

  • 列数に注意
  • 実行可能ファイルと同じフォルダーに schema.ini を配置します。
于 2009-01-22T22:47:24.943 に答える