[英]SqlDataReader while(reader.Read())
private bool ReadMagic(BinaryReader reader)
{
try
{
ini:
byte b0 = reader.ReadByte();
if (b0 != 0xF9) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xbe) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xb4) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xd9) goto ini;
return true;
}
catch (EndOfStreamException)
{
return false;
}
}
我在github上找到了这段代码,想知道为什么有人会使用ReadMagic函数而不是通常的reader.Read()函数? 还可以有人解释ReadMagic函数的工作原理吗?
using(var reader = command.ExecuteReader())
{
while(ReadMagic(reader));
}
SqlDataReader
与BinaryReader
几乎是不一样的,您不会这样使用它。 BinaryReader
用于读取流(例如,从文件读取)。 它具有ReadByte
方法,用于从流中读取实际字节,而SqlDataReader
使用Read
前进到返回的下一行(如果有的话)。
看起来ReadMagic
函数正在消耗流中的字节,直到它获得一系列0xF9、0xBE,0xB4、0xD9-大概是它正在读取的任何流中的某种标记,然后在找到它时返回true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.