C# - Inserting multiple rows using a stored procedure

后端 未结 5 1631
青春惊慌失措
青春惊慌失措 2021-02-15 14:17

I have a list of objects, this list contains about 4 million objects. there is a stored proc that takes objects attributes as params , make some lookups and insert them into tab

5条回答
  •  囚心锁ツ
    2021-02-15 14:25

    I have had excellent results using XML to get large amounts of data into SQL Server. Like you, I initially was inserting rows one at a time which took forever due to the round trip time between the application and the server, then I switched the logic to pass in an XML string containing all the rows to insert. Time to insert went from 30 minutes to less that 5 seconds. This was for a couple of thousand rows. I have tested with XML strings up to 20 megabytes in size and there were no issues. Depending on your row size this might be an option.

    The data was passed in as an XML String using the nText type.

    Something like this formed the basic details of the stored procedure that did the work:

    CREATE PROCEDURE XMLInsertPr( @XmlString ntext )
    DECLARE @ReturnStatus int, @hdoc int

    EXEC @ReturnStatus = sp_xml_preparedocument @hdoc OUTPUT, @XmlString
    IF (@ReturnStatus <> 0)
    BEGIN
    RAISERROR ('Unable to open XML document', 16,1,50003)
    RETURN @ReturnStatus
    END

    INSERT INTO TableName
    SELECT * FROM OPENXML(@hdoc, '/XMLData/Data') WITH TableName
    END

提交回复
热议问题