读者有空白或空记录

时间:2014-03-31 21:14:31

标签: vb.net ms-access

认为代码会捕获空记录,但事实证明它没有,也没有错误。

结果我的函数总是返回FALSE

Try
        conn.Open()
        Dim strQuery As String = "Select * FROM [UsersDataTbl] " & _
                   "WHERE [UserName] = """ & UserName & """"
        Dim comm As New Data.OleDb.OleDbCommand(strQuery, conn)
        Dim reader As Data.OleDb.OleDbDataReader = comm.ExecuteReader()
        While reader.Read()
            If noNull(reader("StudentID") = "") _
            Or noNull(reader("LastName") = "") _
            Or noNull(reader("FirstName") = "") _
            Or noNull(reader("Affiliation") = "") Then
                BlankFields = True
            Else
                BlankFields = False
            End If
        End While
        conn.Close()
    Catch ex As Exception

增加: 找到了我的noNull方法:

 Public Function noNull(ByRef o As Object) As String
    If (o Is Nothing) Then
        Return ""
    End If
    Return o.ToString()

End Function

3 个答案:

答案 0 :(得分:1)

我处理这样的记录字段值:

Dim iVal As Integer = NoNull(r.Fields("someintegerfield").Value, "0", False)


Public Function NoNull(ByVal uAny As Object, Optional ByVal uFillString As String = "", Optional ByVal uTreatDecimalNullAsNothing As Boolean = False) As String

    Dim sRet As String = String.Empty

    If Not Convert.IsDBNull(uAny) AndAlso Not uAny Is Nothing Then
        Debug.Assert(uAny.GetType.ToString <> "cField") 'checking if the argument is a "cField" helps me to check whether I passes "r.fields("somefield").value to this function, or if I forgot the ".value")

        sRet = uAny
    Else
        sRet = String.Empty
    End If

    If StrLen(sRet) = 0 Then
        If modStrings.StrLen(uFillString) > 0 Then
            sRet = uFillString
        End If
    End If

    If uTreatDecimalNullAsNothing Then
        If sRet = "0" Then
            sRet = uFillString
        End If
    End If

    Return sRet

End Function

Public Function StrLen(ByVal uString As String) As Integer

    If (Not uString Is Nothing) AndAlso (Not uString = "") Then
        Return uString.Length
    Else
        Return 0
    End If

End Function

答案 1 :(得分:0)

我希望这可以帮到你。

Dim con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\yourDB.accdb;")
Dim cb As String = "SELECT * FROM Table1 "
Dim dr As System.Data.OleDb.OleDbDataReader
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = con
cmd.CommandText = cb
con.Open()
dr = cmd.ExecuteReader
   While dr.Read()
        If Not IsDBNull(dr("value1")) Then MessageBox.Show(dr("value1"))
    End While
con.Close()

答案 2 :(得分:0)

假设noNull方法是这样的

Public Function noNull(dbValue As Object) as Boolean
    if dbValue = DBNull.Value OrElse dbValue = "" Then
         return True
    else
         return False
    End If
End Function

然后用

调用它
....
noNull(reader("LastName") = "")
....

这意味着您将LastName字段的值与空字符串进行比较,结果是布尔值True或False,但将布尔值传递给noNull意味着它永远不会相等到空字符串或DBNull.Value,因此它将始终返回false

您需要使用

调用方法
       If noNull(reader("StudentID")) _
        Or noNull(reader("LastName")) _
        Or noNull(reader("FirstName")) _
        Or noNull(reader("Affiliation")) Then

或没有noNull方法

       If reader.IsDBNull(reader.GetOrdinal("StudentID")) _
                        OrElse reader("StudentID") = ""  _
                        OrElse reader.IsDBNull(reader.GetOrdinal("LastName")) _
                        OrElse reader("LastName") = ""
                        OrElse reader.IsDBNull(reader.GetOrdinal("FirstName")) _
                        OrElse reader("FirstName") = "" _
                        OrElse reader.IsDBNull(reader.GetOrdinal("Affiliation")) _
                        OrElse reader("Affiliation") = "" Then
           BlankFields = True
      Else
           BlankFields = False
      End If

正如你所看到的那样,这真的很难看,所以我认为上面提到的像noNull这样的方法在这种情况下可能很有用

编辑现在,查看noNull方法的代码,然后很清楚错误在哪里。 你应该只改变括号位置。

If noNull(reader("StudentID")) = "" _
          Or noNull(reader("LastName")) = "" _
          Or noNull(reader("FirstName")) = "" _
          Or noNull(reader("Affiliation")) = "" Then