VB.NET:如何使用ExecuteSQLCommand并在数据表中返回我的数据并访问它?

时间:2011-11-04 12:56:11

标签: asp.net sql vb.net sqlcommand

这就是我正在尝试的......

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 ?

2 个答案:

答案 0 :(得分:5)

这是一个没有数据表的示例,如您的评论中所述。

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()

为了与标题匹配的完整答案,使用数据集:

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

数据表:

Dim resultTable as DataTable = ds.Tables(0)

答案 1 :(得分:2)

要将结果放在DataTable中,您需要在SqlDataAdapter周围创建SqlCommand并调用其Fill方法。