what is the best way to insert,update batch records from code behind to ms sql?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In terms of INSERTs, SqlBulkCopy is the fastest way to bulk load data into SQL Server. I’ve blogged about how to use it/demonstrated the performance here – comparing to the other approach of using an SqlDataAdapter to send batched inserts via SqlDataAdapter.Update in conjunction with an SqlDataAdapter.InsertCommand.
In terms of UPDATEs, one technique is to bulk load the data into a “temporary” staging table in the database using SqlBulkCopy. And then, run an update on your underlying table from this staging table. Alternatively, you can use the SqlDataAdapter.Update approach, in conjunction with the SqlDataAdapter.UpdateCommand
For raw throughput, SqlBulkCopy (INSERTs only) is the ideal way. However, for handling errors with specific records, the SqlDataAdapter approach is good because you can tell it to continue sending rows to the DB in the event that one fails (e.g. say you get a constraint error on a particular record, you can choose to ContinueUpdateOnError, and then at the end identify those that did error.