简体   繁体   中英

C# Data Table Type problem on importing from CSV

I am using the following

string sql = @"SELECT * FROM [" + fileName + "]";

using (OleDbConnection connection = new OleDbConnection(
                  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                  ";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
    DataTable dataTable = new DataTable();
    dataTable.Locale = CultureInfo.CurrentCulture;
    adapter.Fill(dataTable);

    intCurrentTableRowCount = dataTable.Rows.Count;

    return dataTable;
}

The problem is the 12,13,14 columns are returned as a date field (which would be no problem but some of dates are European ie 24/12/2021) so those data table fields are blank.

Is there a way around this issue? Not even sure where to start.

You cast your column in varchar. If you need datetime in your code, after import you can add new column in the dataset and in a loop use Datetime.TryParse for parse the date.

Is not the most performant solution but it s will works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM