Vincent
Vincent

Reputation: 13

Get multiple dataTables from a sqlDataAdapter

I'm not even sure what I'm asking is even possible, but here goes:

Is there a way to loop through a sqlDataAdapter to find multiple tables?

Currently I have a module that you pass in an object and it automatically fills the parameters of a stored procedure with the values of the object. It works great, except when the stored procedure returns more than one table, it only returns the first one.

Here is what I tried to get to that second table:

Dim ds As New DataSet
Dim table As New DataTable
Dim reader As SqlDataReader = command.ExecuteReader
While reader.IsClosed = False
        reader.Read()
        Try
            table.Load(reader)
            If Not ds.Tables.Contains(table.TableName) Then
                ds.Tables.Add(table)
            End If
        Catch ex As Exception
        End Try
    End While

Any help / ideas would be helpfull!

Upvotes: 1

Views: 4140

Answers (1)

Mike Hofer
Mike Hofer

Reputation: 17002

You don't want a SqlDataReader. You want a SqlCommand, A SqlDataAdapter, and rigorous use of the using statement:

Using connection As New SqlConnection(myConnectionString)
    connection.Open()
    Using command As New SqlCommand(connection)
        command.CommandText = "myStoredProcedure"
        command.CommandType = CommandType.StoredProcedure
        Using adapter = new SqlDataAdapter(command)
            Dim dataset As new DataSet()
            adapter.Fill(dataSet)
            For Each table As DataTable In dataset.Tables
                ' Do something spectacular
            Next
        End Using
    End Using
End Using 

Upvotes: 3

Related Questions