0

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
5
  • Read about using a Common Table Expression Commented Mar 22, 2016 at 14:58
  • 4
    That's because subsequent selects are actually in a different result set. Your reader needs to do a .NextResult after each read. Commented Mar 22, 2016 at 15:04
  • Why the rbar? What's wrong with a single select and BETWEEN operator? Commented Mar 22, 2016 at 18:25
  • @Ghost thank you that's exactly what I wanted. Commented Mar 23, 2016 at 7:24
  • @mxix My select is more complicated than the code I posted. I give the SQL script a XML with different numbers of parameters, which are used in the where clause. Commented Mar 23, 2016 at 7:24

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.