It seems like IDataReader.Read() is always true at least one time (If I'm wrong about this let me know.) So how do you tell if it has no records without just wrapping it in a try/catch?
JC Grubbs
问问题
18802 次
4 回答
23
if(dr.Read())
{
//do stuff
}
else
{
//it's empty
}
usually you'll do this though:
while(dr.Read())
{
}
于 2008-09-09T02:14:40.613 回答
6
Yes, if you want to use the interface then Read until false is the only way to test. If you are looking for a generic IDataReader
implementation, you could try DbDataReader
and use the HasRows
property.
于 2008-09-09T02:15:31.643 回答
3
你可以System.Data.IDataReader
投到System.Data.Common.DbDataReader
using (System.Data.IDataReader IReader = ICommand.ExecuteReader())
{
if (((System.Data.Common.DbDataReader)IReader).HasRows)
{
//do stuff
}
} // End Using IReader
这是纯粹的邪恶,但它(通常)有效;)
(假设您的实例IDataReader
是由自定义 ADO.NET 提供程序实现的,而不是您的某些自定义愚蠢类,它只是实现IDataReader
而不是从DbDataReader
[which implements IDataReader
] 派生)。
于 2013-04-22T12:06:10.907 回答
2
刚刚偶然发现这个问题并想出了这个......
bool isBeforeEoF;
do
{
isBeforeEoF = reader.Read();
if (isBeforeEoF)
{
yield return new Foo()
{
StreamID = (Guid)reader["ID"],
FileType = (string)reader["Type"],
Name = (string)reader["Name"],
RelativePath = (string)reader["RelativePath"]
};
}
} while (isBeforeEoF);
于 2015-04-23T16:35:00.843 回答