Home >Database >Mysql Tutorial >Can FastMember Eliminate the Need for IDataReader with SqlBulkCopy for Bulk Insertion from a List of Objects?
Bulk Insertion with SqlBulkCopy from a List of Objects
When faced with the task of performing a substantial insertion into a database from a list of simple objects, one may wonder about utilizing SqlBulkCopy to accomplish this feat. The question arises whether a custom implementation of the IDataReader interface is necessary for such an operation.
This query can be readily answered by exploring the capabilities of FastMember. By employing this library, you can execute bulk insertions without the need for an intermediary DataTable. This approach not only simplifies the procedure but also enhances performance, as demonstrated by empirical testing.
In practice, the implementation using FastMember transpires as follows:
using(var bcp = new SqlBulkCopy(connection)) using(var reader = ObjectReader.Create(data, "Id", "Name", "Description")) { bcp.DestinationTableName = "SomeTable"; bcp.WriteToServer(reader); }
It is noteworthy that ObjectReader can accommodate both generic and non-generic data sources. Moreover, specifying the member names in advance is not a strict requirement. However, exploiting SqlBulkCopy's ColumnMappings feature can prove advantageous in situations where manual specification is omitted.
The above is the detailed content of Can FastMember Eliminate the Need for IDataReader with SqlBulkCopy for Bulk Insertion from a List of Objects?. For more information, please follow other related articles on the PHP Chinese website!