0

我正在研究在此链接中确定的搜索过程......

检查 VB.net 的 DataTable 中是否存在值的最简单/最快的方法?

我想使用这种搜索技术,但想知道是否有任何方法可以将两列添加到流程中......

Shared Function CheckValue(myTable As DataTable, columnName As String, searchValue As String) As Boolean
For row As DataRow In myTable.Rows
    If row(columnName) = searchValue Then Return True
    Next
    Return False
End Function

我不能使用 PK,但想匹配两个字段,一个是字符串,另一个是整数。

谢谢,

4

2 回答 2

1

如果您想使用该方法在 2 列中搜索 2 个值,则:

Shared Function CheckValue(myTable As DataTable, columnName1 As String, searchValue1 As String, columnName2 As String, searchValue2 As Integer) As Boolean
  For row As DataRow In myTable.Rows
    If row(columnName1) = searchValue1 And row(columnName2) = searchValue2 Then Return True
  Next
  Return False
End Function

请注意,虽然上面不检查 DBNull 值。如果遇到一个,就会抛出异常。为了迎合 DBNull,需要修改 if 语句。

于 2013-05-16T15:33:48.727 回答
0

或者您可以从选择中获取一组行

Dim oRowsInTarget As DataRow() = MyTable.Select("[Col1Name] = '" & SearchVal1 & "' AND [Col2Name] = '" & SearchVal2 & "'")
IF oRowsInTarget.count > 0 Then
    'Do something
End

或在单列中

If MyTable.Select("[Col1Name] = '" & SearchVal1 & "' AND [Col2Name] = '" & SearchVal2 & "'").Count > 0 Then Return True
于 2013-05-16T15:55:35.447 回答