phxsoftware System.Data.SQLite DbDataReader 行为不当

发布于 2024-07-23 21:38:49 字数 1079 浏览 12 评论 0原文

以下帖子与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2024-07-30 21:38:49
  1. 运行 ExecuteReader 后,可以这样想,集合位于第 -1 行。 您需要执行 Read 才能到达第 0 行。

  2. IDataReader 是一个只向前的结构,您只能迭代它一次,调试器正在迭代它。

一般问题:

  • 为什么要锁?
  • 为什么对阅读器进行 null 检查 - 我不知道 ExecuteReader 在选择后返回 null 的任何问题。
  • 为什么不“SELECT SUM(col1) from table1
  • 为什么你不遵循处置模式?
  1. 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.

  2. IDataReader is a forward only structure, you can only iterate through it once, the debugger is iterating through it.

General questions:

  • Why the lock?
  • Why the null check for reader - I am not aware of any issues where ExecuteReader return null after a select.
  • Why not "SELECT SUM(col1) from table1
  • Why are you not following the dispose pattern?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文