gpt4 book ai didi

c# - 如何在不执行 Read() 的情况下在 C# 中检测 DataReader 上的 EOF

转载 作者:太空狗 更新时间:2023-10-29 23:17:44 25 4
gpt4 key购买 nike

我熟悉使用 .Read() 来检测 EOF

        using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders"))
{
AssertOrder(reader);

while (reader.Read())
{
yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase);
}
reader.Close();
}

由于我遇到了一些奇怪的情况,FillRecord 实际上插入了读者。所以现在 while 循环中的 .Read() 实际上导致该函数跳过一些行——因为我们前进了两次。

我希望有一个 IDataReader.EOF,但没有。有什么想法吗?

最佳答案

我想我可能在评论中表达了我的观点......但是,既然你问了“任何想法”......就这样吧:

显然是两秒钟的黑客工作,但你会明白的:

(你肯定想要实现 IDisposable(因为我认为 IDataReader 是??)等。可能只是让类继承自 IDataReader,并将该类实现为外观。或者变得非常酷并实现透明代理劫持 Read 方法。

public class DataReaderWithEOF
{
public bool EOF { public get; private set; }
private IDataReader reader;

public DataReaderWithEOF(IDataReader reader)
{
this.reader = reader;
}

public bool Read()
{
bool result = reader.Read();
this.EOF = !result;
return result;
}
}

关于c# - 如何在不执行 Read() 的情况下在 C# 中检测 DataReader 上的 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7844355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com