如何最好地使用 C# DbDataReader 循环一批结果

发布于 2024-07-16 10:26:13 字数 1120 浏览 1 评论 0原文

我正在批量执行多个 SQL 查询,然后批量获取所有结果集。 我的代码当前组合在一起的方式,第一个结果集被跳过。 现在我知道了这一点,我可以简单地在循环之外添加另一个语句来获取第一个结果,但是我想知道是否有一个更优雅的解决方案来解决这个问题。

下面是一些正在发生的事情的 sudo 代码:

DbDataReader reader= /*some stuff that returns a batch of results...*/;

while (reader.NextResult())
{
   while (reader.Read())
   {
       if (!reader.IsDBNull(0))
       {
           //do things with the data....
       }
   }
}

现在我预计 NextResult() 会在您第一次调用它时将您带到第一个结果,这就是 Read() 似乎要做的事情。 然而,它实际上似乎是在第一次调用时将您带到第二个结果。 我是否误解了您期望如何使用此方法,或者您真的期望执行以下操作:

DbDataReader reader= /*some stuff that returns a batch of results...*/;

//this deals with the row in the the very first result
while (reader.Read())
{
    if (!reader.IsDBNull(0))
    {
        //do things with the data....
    }
}

//this deals with the rest of the rows...
while (reader.NextResult())
{
   while (reader.Read())
   {
       if (!reader.IsDBNull(0))
       {
           //do exact same things with the data....
           //is this not pretty klugey?
       }
   }
}

这让我觉得这是一种糟糕的编程风格,但我没有找到解决方法。 有谁知道对此有更优雅的解决方案吗?

I'm executing a number of SQL query's as a batch and then getting all of the result sets back in a batch. The way that my code is currently put together, the first result set gets skipped. Now that I know this, I could simply throw in another statement outside of my loop that grabs the first result, however I'm wondering if there is a more elegant solution to this problem.

Here is some sudo code of whats going on:

DbDataReader reader= /*some stuff that returns a batch of results...*/;

while (reader.NextResult())
{
   while (reader.Read())
   {
       if (!reader.IsDBNull(0))
       {
           //do things with the data....
       }
   }
}

Now i would have expected that NextResult() advances you to the first result the first time you call it, which is what Read() seems to do. However what it actually seems to do is bring you to the second result on the first call. Am I misunderstanding how you're expected to use this method, or are you really expected to do some like the following:

DbDataReader reader= /*some stuff that returns a batch of results...*/;

//this deals with the row in the the very first result
while (reader.Read())
{
    if (!reader.IsDBNull(0))
    {
        //do things with the data....
    }
}

//this deals with the rest of the rows...
while (reader.NextResult())
{
   while (reader.Read())
   {
       if (!reader.IsDBNull(0))
       {
           //do exact same things with the data....
           //is this not pretty klugey?
       }
   }
}

This strikes me as rotten programming style, but I don't see a way around it. Does anyone know of a more elegant solution to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

铁轨上的流浪者 2024-07-23 10:26:13

只需将 NextResult 放在循环的末尾而不是开头:

do {
   while (reader.Read()) {
      if (!reader.IsDBNull(0)) {
         //do things with the data....
      }
   }
} while (reader.NextResult());

Simply put the NextResult at the end of the loop instead of the beginning:

do {
   while (reader.Read()) {
      if (!reader.IsDBNull(0)) {
         //do things with the data....
      }
   }
} while (reader.NextResult());
⒈起吃苦の倖褔 2024-07-23 10:26:13

在绝大多数情况下,您只会通过任何给定的调用返回一个结果集,因此设计者每次使用读取器时都需要“NextResultSet”是没有意义的。

因此,如果您正在拉取多个结果集,那么您的第二个示例确实有效。 不过,你的帖子让我想知道的另一件事是,如果你检索多个结果集,为什么你会对数据做“完全相同的事情”——数据的结构不会足够不同,以至于你< em>不会做同样的事情吗?

也就是说,你的例子让我想知道你在思考数据管理功能如何工作时是否没有某种错误。

In the great majority of cases, you will only be returning a single result set with any given call so it would not make sense for the designers to have required a "NextResultSet" every time you use a reader.

Thus, your second example does indeed hold if you are pulling multiple result sets. The other thing that your post makes me wonder, though, is why you'd be doing the "exact same things with the data" if you are retrieving multiple result sets - wouldn't the structure of the data be different enough that you wouldn't be doing the exact same things?

That is, your example makes me wonder if you don't have some kind of bug in your thinking about how the data management functions work.

荒路情人 2024-07-23 10:26:13

我通常这样做:

if(reader.HasRows)
    {
      while(reader.Read())
       {

          // Do Stuff
       }
    }

希望有帮助

I usually do this:

if(reader.HasRows)
    {
      while(reader.Read())
       {

          // Do Stuff
       }
    }

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文