c# – 如何在DataReader上实现Peek()函数?
发布时间:2020-12-15 08:34:39 所属栏目:百科 来源:网络整理
导读:ado.net中的DataReader似乎没有Peek方法.我希望能够在循环阅读器之前执行一些一次性处理,能够查看第一行中的数据而不会导致后续迭代跳过它会很好.完成此任务的最佳方法是什么? 我使用的是SqlDataReader,但最好是实现尽可能通用(即应用于IDataReader或DbData
ado.net中的DataReader似乎没有Peek方法.我希望能够在循环阅读器之前执行一些一次性处理,能够查看第一行中的数据而不会导致后续迭代跳过它会很好.完成此任务的最佳方法是什么?
我使用的是SqlDataReader,但最好是实现尽可能通用(即应用于IDataReader或DbDataReader). 解决方法
我建议类似于Jason的解决方案,但使用实现IDataReader的包装器,所以:
sealed public class PeekDataReader : IDataReader { private IDataReader wrappedReader; private bool wasPeeked; private bool lastResult; public PeekDataReader(IDataReader wrappedReader) { this.wrappedReader = wrappedReader; } public bool Peek() { // If the previous operation was a peek,do not move... if (this.wasPeeked) return this.lastResult; // This is the first peek for the current position,so read and tag bool result = Read(); this.wasPeeked = true; return result; } public bool Read() { // If last operation was a peek,do not actually read if (this.wasPeeked) { this.wasPeeked = false; return this.lastResult; } // Remember the result for any subsequent peeks this.lastResult = this.wrappedReader.Read(); return this.lastResult; } public bool NextResult() { this.wasPeeked = false; return this.wrappedReader.NextResult(); } // Add pass-through operations for all other IDataReader methods // that simply call on 'this.wrappedReader' } 请注意,对于所有未受影响的属性,这确实需要相当多的传递代码,但好处是它是一个通用的抽象,可以“窥视”结果集中的任何位置,而无需继续前进的“读取”操作. 使用: using (IDataReader reader = new PeekDataReader(/* actual reader */)) { if (reader.Peek()) { // perform some operations on the first row if it exists... } while (reader.Read()) { // re-use the first row,and then read the remainder... } } 请注意,如果前一个操作不是’Peek()’,则每个’Peek()’调用实际上将移动到下一个记录.使用’Read()’操作保持这种对称性提供了更简单的实现和更优雅的API. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- c – Visual Studio警告关于不同内容的文件副本
- Swift开发:Swift 修改UISegmentedControl字体大
- c# – 嵌套数组循环迭代
- acdream 1211 Reactor Cooling 【上下界网络流 +
- 转 -- 2012年 PostgreSQL中国用户大会演讲主题征
- 未能加载文件或程序集“Newtonsoft.Json, Versio
- ruby-on-rails-3 – 没有路由匹配[GET]“/ auth
- ruby-on-rails – Errno :: EIO:输入/输出错误
- zoj 2314 Reactor Cooling(无源汇上下界可行流)
- oracle密码过期
热点阅读