.net: Converting a Generic List to a DataReader?

.net generic-list sqlbulkcopy sqldatareader winforms

Question

Hi I have a Populated Generic List in a winform and now wanna to send its data into a SQL server table as a bulk Copy. As I read the SqlBulkCopy Class, it needs a data reader as a source.

Now, how can I convert it to a DataReader?

Thank you

Accepted Answer

You can convert it to DataTable and send to SqlBulkCopy. It would be much easier, and there is no performance penalty, because all data is already present in memory.

There is similar question on SO.


Expert Answer

If the data-volume is sane enough to exist as a List<T>, you might as well just construct a DataTable and push the data in that way. I don't like DataTable, but it does this job nicely. The alternative would be to write a custom IDataReader implementation like this, but which iterates over the items in the list each time. I'm not sure it is worth the effort.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why