Using SQLBulkCopy to Insert/Update database

前端 未结 4 1811
遥遥无期
遥遥无期 2020-12-10 14:51

I have a datatable with the records.I\'m inserting records into Sql table using SqlBulkCopy.It works fine.Next time when get the datatable with same records with few changed

4条回答
  •  眼角桃花
    2020-12-10 15:34

    Like mentioned by AdaTheDev, SqlBulkCopy can only insert however there is an alternative library which allow to perform Upsert operations.

    Disclaimer: I'm the owner of the project Bulk Operations

    The Bulk Operations library has a method "BulkMerge" which Insert or Update rows based on the specified key.

    var bulk = new BulkOperation(connection);
    
    bulk.ColumnMappings.Add("ID", true);
    bulk.ColumnMappings.Add("Column1");
    bulk.ColumnMappings.Add("Column2");
    bulk.ColumnMappings.Add("Column3");
    
    bulk.BulkMerge(dt);
    

提交回复
热议问题