开发者

first column value in a datatable

I have a dataset with just 1 datatable and 1 row but with 2 columns. I want to get the value of the 1st column. How can I get开发者_运维知识库 it in vb.net


All you need is

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

where ds is the name of your DataSet object. This will return the first column from the first row of the first table as an Object.


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


Try with:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜