daveomcd
daveomcd

Reputation: 6555

VB.NET: How can I use ExecuteSQLCommand and return my data in a datatable and access it?

This is what I'm trying...

Dim myCommand as SqlCommand
Dim myTable as New xsdObject.ObjectDataTable

myCommand = DataAccess.GetSQLCommand( "MyStoredProc", _
                       CommandType.StoredProcedure, SourceServer.ConnectionLocal)

myCommand.Connection.ChangeDatabase("MyDatabase")
myCommand.Parameters.AddWithValue("@Parameter", DataValue)

ExecuteSQLCommand(myCommand, myTable)

 'How to access my data ?

Upvotes: 0

Views: 3769

Answers (2)

Jay
Jay

Reputation: 6017

Here is an example without the datatable, as mentioned in your comment.

Dim reader As SqlDataReader= MyCommand.ExecuteReader
    Dim MyList as new List(Of WhateverObject)
    Do While reader.Read
        Dim obj as new WhateverObj
        obj.Property1 = Reader.GetInt32(0)
        obj.Property2 = Reader.GetString(1)
        MyList.add(obj)
    Loop

    reader.Close()
    myCommand.Dispose()

For the sake of complete answer that matches the title, with a dataset:

    Dim Adapter As New SqlDataAdapter(myCommand)
    Dim ds As DataSet = New DataSet
    Adapter.Fill(ds)

The Datatable:

Dim resultTable as DataTable = ds.Tables(0)

Upvotes: 5

SLaks
SLaks

Reputation: 887195

To put the results in a DataTable, you need to create a SqlDataAdapter around the SqlCommand and call its Fill method.

Upvotes: 2

Related Questions