2010-03-26 48 views

回答

9

所有你需要的是

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

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

+0

+1。也适用于'DataSet's – 2013-01-17 20:16:39

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 
+0

他只有一行,他只想要第一列的值。 – 2010-03-26 20:44:33

+0

@jmgant - 完全同意。试图为他的下一个场景放置东西:)当更多的行可能会来。 – 2010-03-26 20:49:22

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 
+0

thisReader.ExecuteScalar在这里是一个更好的选择。无需打开阅读器即可阅读单个值。 但是,如果我正在阅读正确的问题,他不想连接到数据库。 – 2010-03-26 20:47:42

+0

你说的都对:)。 – systempuntoout 2010-03-26 21:05:52

相关问题