SqlBulkCopy from a List<>

前端 未结 4 1106
无人共我
无人共我 2020-11-27 02:53

How can I make a big insertion with SqlBulkCopy from a List<> of simple object ?

Do I implement my custom IDataReader ?

4条回答
  •  失恋的感觉
    2020-11-27 03:20

    With FastMember, you can do this without ever needing to go via DataTable (which, in my tests, more-than-doubles the performance):

    using(var bcp = new SqlBulkCopy(connection))
    using(var reader = ObjectReader.Create(data, "Id", "Name", "Description"))
    {
        bcp.DestinationTableName = "SomeTable";
        bcp.WriteToServer(reader);
    }
    

    Note that ObjectReader can also work with non-generic sources, and it is not necessary to specify the member-names in advance (although you probably want to use the ColumnMappings aspect of SqlBulkCopy if you don't specify them in the ObjectReader itself).

提交回复
热议问题