1

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

odiseh
  • 25,407
  • 33
  • 108
  • 151
  • 2
    See this answer for converting a generic list to a DataTable: http://stackoverflow.com/questions/564366/generic-list-to-datatable – Andy Rose Feb 01 '11 at 08:41

2 Answers2

4

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.

Community
  • 1
  • 1
gor
  • 11,498
  • 5
  • 36
  • 42
2

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900