Home >Database >Mysql Tutorial >How Can I Efficiently Insert Large Datasets into SQL Server Using SqlBulkCopy and Lists of Objects?
Inserting Large Data Sets with SqlBulkCopy and Lists
Question: How to efficiently insert large datasets into a database table using SqlBulkCopy when the data source is a list of simple objects?
Response:
Consider leveraging FastMember to simplify the process without relying on intermediate data structures like DataTable. This can significantly improve performance. Here's an example:
using(var bcp = new SqlBulkCopy(connection)) using(var reader = ObjectReader.Create(data, "Id", "Name", "Description")) { bcp.DestinationTableName = "SomeTable"; bcp.WriteToServer(reader); }
FastMember's ObjectReader can handle non-generic data sources and supports specifying member names dynamically without explicitly defining them in advance. However, using SqlBulkCopy's ColumnMappings can be beneficial if you don't specify member names in ObjectReader.
The above is the detailed content of How Can I Efficiently Insert Large Datasets into SQL Server Using SqlBulkCopy and Lists of Objects?. For more information, please follow other related articles on the PHP Chinese website!