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

后端 未结 4 2036
有刺的猬
有刺的猬 2021-02-14 08:18

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:09

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

提交回复
热议问题