通用 DataReader 代码示例 (.net)
由于我对 .Net 的体验实际上是从为公司现有的内部应用程序工作开始的,所以我觉得我在没有意识到的情况下已经学到了很多糟糕的编码实践。我拼命想要摆脱的一个问题是数据集用于一切。 (我确实知道强类型数据集在那里,它们当然有一些用途......但不适用于大多数情况,例如选择数据)
我正在为通用数据库工作构建一个“帮助器”类......我'我有一个返回数据表(用于选择等)的方法,我想默认情况下(以及书籍/在线中的大多数示例)会使用 DataAdapter 的 Fill 方法,但当然作为性能增益,希望用数据读取器替换它读取所有行然后关闭...我猜这就是 Fill 方法在底层的工作原理...但如果大型结果集的性能可能会受到影响,我不希望简单地采用惰性方式。
无论如何,我一生都找不到用于一般填充数据表的 dataReader 的示例...我确信会有好的示例和坏的示例,因此就如何执行此类操作达成一致的最佳实践一项任务。此类代码的链接(甚至帖子)会很棒!我主要使用 VB.Net,但 c# 不是障碍。
注意:抱歉,如果这听起来也很懒,我只是想这种例子会到处张贴......不需要重新发明轮子等。谢谢!
As my experience with .Net really began as working on existing in-house applications for a company, I feel I've picked up so many bad coding practices without realising it. One that i am desperately trying to move on from is that DataSets are used for everything. (I do get that strongly typed datasets have there place and they certainly have some uses...but not for the majority of cases e.g. selecting data)
I'm building up a "helper" class for generic database work...I've got a method which returns a data table (for selects etc.) and I guess by default (and most examples in books/online) would use the DataAdapter's Fill method but certainly as a performance gain, want to replace this with a data reader that reads all the rows and then closes...which I guess is how the Fill method works underneath...but I'd prefer not to simply go for the lazy way if performance on large result sets is potentially going to impact.
Anyway, I can't for the life of me find an example of a dataReader being used to generically fill a datatable...I'm sure there would be both good and bad examples and therefore an agreed best practice on how to perform such a task. A link (or even a post) to such code would be brilliant! I'm mostly VB.Net but c# is no obstacle.
Note: Sorry if this sounds lazy also, I just figured this sort of example would be posted everywhere...no need to re-invent the wheel etc. thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您找不到用于一般填充
DataTable
的DataReader
示例的原因是因为您可以使用Fill()< /code> 方法在
DataSet
中,所以你只是重新发明轮子。通过直接使用 DataReader 填充 DataTable 不会获得性能优势。
The reason you can't find an example of a
DataReader
being used to generically fill aDataTable
is because you can do the same thing with theFill()
method in theDataSet
, so you would just be reinventing the wheel.You're not going to find a performance benefit by populating the DataTable directly with a DataReader.
如果我不得不猜测,我认为与使用带有 Fill() 方法的 sqldataadapter 相比,使用 sqldatareader 填充数据表不会有任何性能优势。
真正验证该理论的唯一方法是编写自己的实现并进行比较。但您也可以查看 Fill() 正在执行的代码,以了解它到底在做什么。我建议下载 Reflector 来查看代码。
我刚刚自己做了这个。以下是调用 Fill() 后最终调用的内容:
您会注意到,然后调用了 FillFromReader():
在查看所有这些之后,我可能不得不改变主意。为所有这些编写自己的实现确实可能会带来显着的改进。这些函数中的逻辑比我预期的要多,并且一遍又一遍调用 FillNextResult() 时会产生一些函数调用开销。
If I had to guess, I don't think there will be any performance benefit of using a sqldatareader to fill a datatable compared to using a sqldataadapter with the Fill() method.
The only way to truly verify that theory would be to write your own implementation and compare. But you can also look at the code that Fill() is executing to see what it's doing exactly. I'd suggest downloading Reflector to take a look at the code.
I just did this myself. Here's what is eventually called after you make a call to Fill():
You'll notice that this then makes a call to FillFromReader():
After looking at all of this I may have to change my mind. There may indeed be a measurable improvement to writing your own implementation for all of this. There is more logic in these functions than I expected, as well as some function call overhead when calling FillNextResult() over and over.
下面是一个 dataReader 用于一般填充数据表的示例。您可以简单地使用 DataTable 的 Load 方法并将其传递给 DataReader:
Here is an example of a dataReader being used to generically fill a datatable. You can simply use the Load method of the DataTable and pass it the DataReader: