1

我需要知道何时以及是否存在一行并且我想使用 ExecuteReader 对吗?

我的代码只是为了让我知道我要去哪里。

Const c_str_SELECT_SQL As String = "SELECT Fails FROM LoginAttacks WHERE IP = ?"

If Not String.IsNullOrEmpty(strPointsDBConnectionString) Then
    objOLEConnection = New OleDbConnection(strPointsDBConnectionString)
    objOLEConnection.Open()
    objOLECommand = New OleDbCommand(c_str_SELECT_SQL, objOLEConnection)
    objIP = objOLECommand.Parameters.Add("@IP", OleDbType.Char)
    objIP.Value = Me.IPAddress

    ' Connect execute update query
    FailedCounts = CInt(objOLECommand.ExecuteScalar())

    If FailedCounts > 0 Then 'need to find way to tell if query worked/found row with ip
        FailedCounts = FailedCounts + 1
        'Did not fail assume worked so go use update function
        IPExist = True 'if not go use insert function
    End If
Else
    Throw New Exception(const_CONNECTION_STRING_ERROR)
End If

更新:

我补充说:

objOLEDataReader = objOLECommand.ExecuteReader()

If objOLEDataReader.HasRows Then
    'Do While objOLEDataReader.Read()
    FailedCounts = CInt(objOLECommand.ExecuteScalar())
    FailedCounts = FailedCounts + 1
    'Did not fail assume worked so go use update function
    IPExist = True 'if not go use insert function
    'Loop
Else
    'none found
End If

objOLEDataReader.Close()

但是,即使有行,我似乎也不会进入 IF 语句。

4

1 回答 1

0

文档中,如果没有数据,ExecuteScalar将返回。Nothing所以你应该能够首先检查Nothing是否有行:

Dim FailedCountsObject As Object = objOLECommand.ExecuteScalar()

'need to find way to tell if query worked/found row with ip
If FailedCountsObject IsNot Nothing Then
    FailedCounts = CInt(FailedCountsObject)
    FailedCounts = FailedCounts + 1
    'Did not fail assume worked so go use update function
    IPExist = True 'if not go use insert function
End If
于 2012-04-18T14:36:23.957 回答