数据表中的第一列值

时间:2010-03-26 20:37:35

标签: vb.net

我有一个只有1个数据表和1行但有2列的数据集。 我想获得第一列的价值。 我怎样才能在vb.net中获取它

3 个答案:

答案 0 :(得分:9)

你需要的只是

ds.Tables(0).Rows(0)(0)

其中ds是DataSet对象的名称。这将从第一个表的第一行返回第一列作为对象。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试:

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