【发布时间】:2010-03-27 04:49:18
【问题描述】:
我有一个只有 1 个数据表和 1 行但有 2 列的数据集。 我想获得第一列的值。 如何在 vb.net 中获得它
【问题讨论】:
标签: vb.net
我有一个只有 1 个数据表和 1 行但有 2 列的数据集。 我想获得第一列的值。 如何在 vb.net 中获得它
【问题讨论】:
标签: vb.net
你只需要
ds.Tables(0).Rows(0)(0)
其中 ds 是您的 DataSet 对象的名称。这会将第一个表的第一行的第一列作为对象返回。
【讨论】:
Private Sub PrintValues(ByVal myTable As DataTable)
Dim myRow As DataRow
Dim myColumn As DataColumn
For Each myRow in myTable.Rows
For Each myColumn In myTable.Columns
Console.WriteLine(myRow(myColumn))
Exit For
Next
Next
End Sub
【讨论】:
尝试:
public class MainClass
Shared Sub Main()
Dim thisConnection As New SqlConnection("yourconnection")
Dim thisCommand As New SqlCommand _
("SELECT FirstField FROM YourTable",thisConnection)
Try
thisConnection.Open()
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
While (thisReader.Read())
MessageBox.Show(thisReader.GetValue(0))
End While
Finally
thisConnection.Close()
End Try
End Sub
End Class
【讨论】: