.net:将通用列表转换为 DataReader?
你好 我在 winform 中有一个填充的通用列表,现在想将其数据作为批量复制发送到 SQL Server 表中。当我阅读 SqlBulkCopy 类时,它需要一个数据读取器作为源。
现在,我如何将其转换为 DataReader?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将其转换为DataTable并发送到SqlBulkCopy。这会容易得多,并且不会造成性能损失,因为所有数据都已经存在于内存中。
SO 上也有类似的问题。
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.
如果数据量足够健全,可以作为
List
存在,您也可以构建一个DataTable
并以这种方式推送数据。我不喜欢 DataTable,但它能很好地完成这项工作。另一种方法是编写自定义IDataReader
实现 像这样,但每次都会迭代列表中的项目。我不确定这是否值得付出努力。If the data-volume is sane enough to exist as a
List<T>
, you might as well just construct aDataTable
and push the data in that way. I don't likeDataTable
, but it does this job nicely. The alternative would be to write a customIDataReader
implementation like this, but which iterates over the items in the list each time. I'm not sure it is worth the effort.