SqlBulkCopy Not Working

前端 未结 7 2078
予麋鹿
予麋鹿 2021-02-18 17:54

I have a DataSet populated from Excel Sheet. I wanted to use SQLBulk Copy to Insert Records in Lead_Hdr table where LeadId is PK.

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 18:36

    Well, is it right? Do the column names exist on both sides?

    To be honest, I've never bothered with mappings. I like to keep things simple - I tend to have a staging table that looks like the input on the server, then I SqlBulkCopy into the staging table, and finally run a stored procedure to move the table from the staging table into the actual table; advantages:

    • no issues with live data corruption if the import fails at any point
    • I can put a transaction just around the SPROC
    • I can have the bcp work without logging, safe in the knowledge that the SPROC will be logged
    • it is simple ;-p (no messing with mappings)

    As a final thought - if you are dealing with bulk data, you can get better throughput using IDataReader (since this is a streaming API, where-as DataTable is a buffered API). For example, I tend to hook CSV imports up using CsvReader as the source for a SqlBulkCopy. Alternatively, I have written shims around XmlReader to present each first-level element as a row in an IDataReader - very fast.

提交回复
热议问题