I would combine this CSV reader with SqlBulkCopy
; i.e.
using (var file = new StreamReader(path))
using (var csv = new CsvReader(file, true)) // true = has header row
using (var bcp = new SqlBulkCopy(connection)) {
bcp.DestinationTableName = "TableName";
bcp.WriteToServer(csv);
}
This uses the bulk-copy API to do the inserts, while using a fully-managed (and fast) IDataReader
implementation (crucially, which streams the data, rather than loading it all at once).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…