简体   繁体   中英

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 ?

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)

要将结果放入DataTable ,您需要围绕SqlCommand创建一个SqlDataAdapter并调用其Fill方法。

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