简体   繁体   中英

SqlDataReader read whole query

I have a SQL script with a loop. In each iteration I change the where clause. So I get several selects displayed. But my .net program reads only the first select. The SQL script works in ssms.

This is my SQL script

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

This is my vb.net code

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

That's because subsequent selects are actually in a different result set. Your reader needs to do a .NextResult after each read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM