如何获得' name'的价值? col,同样的方式我得到行数?

时间:2016-05-02 13:46:47

标签: vb.net

数据库名称是' randomTable' 第一个名称是' id' 第二个名称是' name'

如何获得' name'的价值? col,同样的方式我得到行数?

受保护的Sub BindGridData()

' Connect to databse and open database file
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet()
Dim query As String = "SELECT * FROM [randomTable]"
Dim sqlConn As New SqlConnection(myCon)

Try
    sqlConn.Open()

    Dim cmd = New SqlCommand(query, myCon)
    da = New SqlDataAdapter(cmd)
    Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
    da.Fill(ds, "randomTable")

    Dim a As Integer = ds.Tables("randomTable").Rows.Count
    If a = 0 Then
        e.Text = "empty"
    End If

    'this line is wrong - ???
    Dim s As String = ds.Tables("randomTable").Columns("name")
    if s = "value1" 
        'do something
    end if

    GridView1.DataSource = ds.Tables("randomTable").DefaultView
    GridView1.DataBind()

    sqlConn.Close()
Catch ex As Exception
End Try

End Sub

2 个答案:

答案 0 :(得分:0)

如果你想要行Count,你可以执行查询数据库"从randomTable"中选择Count()。但具体你想做什么?

答案 1 :(得分:0)

按特定行索引或迭代行集合,例如

Dim s As String = ds.Tables("randomTable").Rows(0).Field(Of String)("name")
If s = "value1" Then
    'do something
End If

' or

For row As Integer = 0 To ds.Tables("randomTable").Rows.Count
    If ds.Tables("randomTable").Rows(row).Field(Of String)("name") = "value1" Then
        ' do something
    End If
Next