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 に答える