SqlBulkCopy Error handling / continue on error

后端 未结 3 745
花落未央
花落未央 2020-12-06 02:17

I am trying to insert huge amount of data into SQL server. My destination table has an unique index called \"Hash\".

I would like to replace my SqlDataAdapter impl

3条回答
  •  误落风尘
    2020-12-06 02:52

    Slightly different approach than already suggested; Perform the SqlBulkCopy and catch the SqlException thrown:

        Violation of PRIMARY KEY constraint 'PK_MyPK'. Cannot insert duplicate 
    key in object 'dbo.MyTable'. **The duplicate key value is (17)**.
    

    You can then remove all items from your source from ID 17, the first record that was duplicated. I'm making assumptions here that apply to my circumstances and possibly not yours; i.e. that the duplication is caused by the exact same data from a previously failed SqlBulkCopy due to SQL/Network errors during the upload.

提交回复
热议问题