简体   繁体   English

VB.NET:如何使用ExecuteSQLCommand并将数据返回到数据表中并进行访问?

[英]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方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM