質問

C#を使用しています。

テキストファイルをオブジェクトにプルしようとしています。 ODBC接続を使用していますが、このように見えます

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

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

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

テキストファイルは次のようになります。

fullstockn | FULLINFO

<!> quot; 555555 <!> quot; |

Contenu:Neuf Ttudes sur lここにテキストがあります.....

役に立ちましたか?

解決

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

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ファイルのページは非常に便利です。

他のヒント

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

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

末尾の<!> quot; \ <!> quot;が欠落している可能性があります。 dbq属性で...

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

この種のopのコードは常に自分で書いています。以下は、この目的のために私が書いた抽象クラスの例です。必要に応じて変更またはサブクラス化できます

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;
    }
}

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

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

and:

  • 列の数に注意してください
  • schema.iniを実行可能ファイルの同じフォルダーに配置します。
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top