How to add CsvHelper records to DataTable to use for SqlBulkCopy to the database

后端 未结 4 1291
旧时难觅i
旧时难觅i 2021-02-14 08:40

I am trying to read a CSV file with CsvHelper, load each record into a DataTable, and then use SqlBulkCopy to insert the data into a database table. With the current code, I get

4条回答
  •  [愿得一人]
    2021-02-14 09:05

    If I'm not mistaken, you should be able to do it with a lot less code. You don't have to put in into another class before going into the DataTable either.

    while( csv.Read() )
    {
        var row = dt.NewRow();
        foreach( DataColumn column in dt.Columns )
        {
            row[column.ColumnName] = csv.GetField( column.DataType, column.ColumnName );
        }
        dt.Rows.Add( row );
    }
    

提交回复
热议问题