How to Get a Specific Column Value from a DataTable?

前端 未结 6 1920
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 02:08

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has two columns CountryID and CountryName.

I

6条回答
  •  一向
    一向 (楼主)
    2020-12-13 02:19

    Datatables have a .Select method, which returns a rows array according to the criteria you specify. Something like this:

    Dim oRows() As DataRow
    
    oRows = dtCountries.Select("CountryName = '" & userinput & "'")
    
    If oRows.Count = 0 Then
       ' No rows found
    Else
       ' At least one row found. Could be more than one
    End If
    

    Of course, if userinput contains ' character, it would raise an exception (like if you query the database). You should escape the ' characters (I use a function to do that).

提交回复
热议问题