Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest Way to get Data From Array Into SQLServer Database?

Problem: How to most efficiently move data from an array into a SQL Server table.

Details: I have created an array with many rows (typically, about 100,000) and many columns (about 40) in memory in a WinForms application. I need to get this array into a corresponding SQL Server table in the fastest way possible. Right now, I am creating a SqlCommand object, looping through the 100,000 rows in my array, and for each row, assigning the 40 parameters of the command object, then calling ExecuteCommand. It works, but is slow, and surely must not be the most efficient way to do this. Should I be putting all the data from the array into a DataTable, and then somehow sending the data table all at once (I don't know how to do that)? Or some other technique? Write out to a file and use bcp (seems like that wouldn't be any faster, I have not tried it). Any suggestions appreciated!

like image 212
Rob3C Avatar asked Nov 25 '25 10:11

Rob3C


1 Answers

SqlBulkCopy. It would be even better if you could store the stuff in memory as a DataTable because one of the overloads of the WriteToServer() method takes one.

EDIT: Here's an example of how to use the API.

like image 51
Robert C. Barth Avatar answered Nov 27 '25 23:11

Robert C. Barth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!