phxsoftware System.Data.SQLite DbDataReader 行为不当
以下帖子与 phxsoftware 的 System.Data.SQLite 数据提供程序相关 (http://sqlite.phxsoftware.com)
我对 DbDataReader 的 Read 方法和/或 Visual Studio 2008 有一个疑问(并且可能是一个问题)。在许多示例中,我看到类似以下的内容(并且我知道这段代码没有多大意义......但是它有一个目的):
DbDataReader reader = null;
Long ltemp = 0;
lock (m_ClassLock)
{
DbCommand cmd = dbCnn.CreateCommand();
cmd.CommandText = “SELECT col1 FROM table1”;
reader = cmd.ExecuteReader();
if (null != reader)
{
while (reader.Read())
{
ltemp += (long)reader[0];
}
}
reader.Close();
第一个问题 - 我从这个例子中不明白的是,我是否通过预先调用 reader.Read() 第一次通过 while 循环丢失了数据? 例如,如果读取器的值为 (3,5,7,9),则从 cmd.ExecuteReader() 返回的读取器最初应该指向 3,对吗? 然后,在 while 循环内的后续调用中,reader.Read() 将移动到 5、7 和 9。 但是,因为 reader.Read() 在第一个“ltemp += ...”行之前被调用,所以我是否跳过第一个结果(3)?
第二个问题 - (我开始认为这可能是 VS 中的一个错误)如果我在调试器中单步执行这组代码,当我停在“if (null != ...”行上的断点处时,我可以清楚地看到鼠标悬停在弹出窗口中并向下钻取,读取器分配了多个行数据值但是,如果我关闭该弹出信息,然后尝试将其恢复,当我向下钻取时,我现在会看到该行。 “枚举没有结果”,之前有明确的数据,
有人可以解释这种行为吗?
The following post relates to the System.Data.SQLite data provider by phxsoftware (http://sqlite.phxsoftware.com)
I have a question (and possibly a problem) with DbDataReader’s Read method and/or Visual Studio 2008. In many examples I see things like the following (and I know this code doesn't make a lot of sense ... but it serves a purpose):
DbDataReader reader = null;
Long ltemp = 0;
lock (m_ClassLock)
{
DbCommand cmd = dbCnn.CreateCommand();
cmd.CommandText = “SELECT col1 FROM table1”;
reader = cmd.ExecuteReader();
if (null != reader)
{
while (reader.Read())
{
ltemp += (long)reader[0];
}
}
reader.Close();
First question - What I dont understand from this example is am I missing data the first time through the while loop by calling reader.Read() upfront? For instance, if the reader has values (3,5,7,9) the returned reader from cmd.ExecuteReader() should be pointing at 3 initially, correct? reader.Read() would then move to 5, 7, and 9 on subsequent invocations within the while loop. But, because reader.Read() is invoked before the first "ltemp += ..." line am I skipping past the first result (3)?
Second question - (and I'm starting to think this might be a bug in VS) If I step through this set of code in the debugger when I stop at a breakpoint on the "if (null != ..." line I can clearly see mu mousing over and drilling down in the popup that reader has multiple row data values assigned to it. However, if I close that popup information, and then try to bring it back up, when I drill down I now see the line "Enumeration yielded no results" where there was clearly data before.
Can anyone explain this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行 ExecuteReader 后,可以这样想,集合位于第 -1 行。 您需要执行 Read 才能到达第 0 行。
IDataReader 是一个只向前的结构,您只能迭代它一次,调试器正在迭代它。
一般问题:
Think about it like this after you run ExecuteReader the set is on row -1. You need to execute Read to get to row 0.
IDataReader is a forward only structure, you can only iterate through it once, the debugger is iterating through it.
General questions: