【问题标题】:SqlDataReader read whole querySqlDataReader 读取整个查询
【发布时间】:2016-03-22 14:52:11
【问题描述】:

我有一个带有循环的 SQL 脚本。在每次迭代中,我都会更改 where 子句。所以我得到了几个选择。但我的 .net 程序只读取第一个选择。 SQL 脚本在 ssms 中运行。

这是我的 SQL 脚本

while (@aktuellParam <= @countParam)
Begin
  SELECT name1,
         name2
  FROM dbo.tableName
  WHERE name like @var
  SET @aktuellParam = aktuellParam+1
END

这是我的 vb.net 代码

Using reader As SqlClient.SqlDataReader = _server.ConnectionContext.ExecuteReader(script)
    Dim lfdnr As Integer = 1

    Do While reader.Read()
        spaltenListe.Add(New ISpalten With
                                 {
                                     .Name1= reader.GetString(reader.GetOrdinal("name1")),
                                     .Name2= reader.GetString(reader.GetOrdinal("name2"))
                                 })
        lfdnr = lfdnr + 1
    Loop
    reader.Close()
End Using

【问题讨论】:

  • 了解如何使用公用表表达式
  • 那是因为后续的选择实际上在不同的结果集中。您的读者需要在每次阅读后执行 .NextResult。
  • 为什么是 rbar?单个 select 和 BETWEEN 运算符有什么问题?
  • @Ghost 谢谢你,这正是我想要的。
  • @mxix 我的选择比我发布的代码更复杂。我给 SQL 脚本一个带有不同数量参数的 XML,这些参数在 where 子句中使用。

标签: sql sql-server vb.net sqldatareader


【解决方案1】:

那是因为后续的选择实际上是在不同的结果集中。您的读者需要在每次阅读后执行 .NextResult。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多