Home > Software design >  OleDbDataReader attaches non-ascii charaters when reading from csv file
OleDbDataReader attaches non-ascii charaters when reading from csv file

Time:08-26

I have the code that successfully connects to a csv file and reads the data into an OleDbDataReader. The only issue is that it attaches some weird characters ("") in the beginning of the file. If I open the file as text, I do not see any strange character.

How can I avoid getting those characters into my DataReader?

Below is the code I use:

connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0; Data Source='{SourceFileDirectory}'; Extended Properties = \"text;HDR=YES;FMT=Delimited(,);\"";
sql = $"select * From [{fileName}]";

using (var oledbConn = new OleDbConnection(connectionString))
{
    using (var cmd = oledbConn.CreateCommand())
    {
        oledbConn.Open();
        cmd.CommandText = sql;

        using (OleDbDataReader reader = cmd.ExecuteReader())
        {
            for (int i = 0; i < reader.FieldCount; i  )
            {
                string columnName = reader.GetName(i);
            }
        }
    }
}

CodePudding user response:

I think it's the charset encoding of the file.

You should check the encoding from a program like Notepad and then change it (UTF-8 should be ok)

By the way, I suggest you to use File.ReadAllLines method and split each lines by the separator

  • Related