当我使用下面的代码时,有人能告诉我为什么会出现以下错误。 从类型转换为DBNull'输入'布尔'无效。
Dim newOrderID As Guid = Guid.Parse(orderID)
Dim DBConnect2 As New DBConn
Using db As DbConnection = DBConnect2.Conn("DBConnectionString")
Dim cmd As SqlCommand = DBConnect2.Command(db, "SelectWebLinkVisible")
cmd.Parameters.Add(New SqlParameter("orderID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = newOrderID
db.Open()
Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim webLinkVisible As Boolean
While DR.Read
webLinkVisible = DR("webLinkVisible")
End While
DR.Close()
DR = Nothing
cmd.Dispose()
cmd = Nothing
db.Dispose()
db.Close()
End Using
我甚至尝试用以下内容替换WHILE位,并认为页面会重定向,因为它尝试读取的数据肯定是NULL但它没有做任何事情。
While DR.Read
If DR("webLinkVisible") is nothing Then
Response.Redirect("www.bbc.co.uk")
End If
End While
答案 0 :(得分:2)
当字段包含NULL值时,OleDbDataReader返回值DBNull.Value。这与Nothing不同。
要处理此方案,您可以使用IsDBNull方法
.then()
需要额外调用DR.GetOrdinal,因为IsDBNull不能使用字段名称,但它需要查询返回的字段的序号位置。