【问题标题】:Visual Basic 2012, having trouble with how to write DBNull. "Conversion from type 'DBNull' to type 'String' is not valid."Visual Basic 2012,在编写 DBNull 时遇到问题。 “从类型 'DBNull' 到类型 'String' 的转换无效。”
【发布时间】:2014-07-30 11:30:58
【问题描述】:

我遇到了上面列出的错误,我在其他地方读到我必须做的是a DBNull-check...,但我不知道该怎么做。我想知道是否有人可以帮助我?我是 VB 的超级新手。

我也尝试将 LedgerId 和 Net 更改为小数,但它告诉我这一行:

JVNet += Net

错了。 “+ 对 DBNull 和小数无效”之类的内容。

这是相关代码(我认为)。

    Dim SQLStmt, LedgerId, NetDisp As String
    Dim Net, JVNet As Decimal
    Dim NetCounter As Integer
    Cnxn.Open()
    SQLStmt = "SELECT LedgerId, sum(Qty * Each) as Net FROM Orders, Details  where CONVERT(nvarchar(10), AcctgDate, 101) = '" & _
        cbJVDate.Text & _
        "' and Orders.Id = Details.OrderId group by LedgerId order by LedgerId"
    Dim JVCommand As New SqlCeCommand(SQLStmt, Cnxn)
    Dim JVReader As SqlCeDataReader
    lblAdvice.Text = ""
    Debug.Print(vbCrLf & "Got Here with '" & SQLStmt & "'" & vbCrLf)
    Try
        JVReader = JVCommand.ExecuteReader
    Catch ex As Exception
        MsgBox("Execute JVReader with '" & SQLStmt & "' got " & ex.Message)
        Exit Sub
    End Try
    JVNet = 0
    NetCounter = 0
    lbJournal.Items.Clear()
    Do While JVReader.Read()
        LedgerId = JVReader.Item("LedgerId")
        Net = JVReader.Item("Net")
        JVNet += Net
        NetCounter += 1
        NetDisp = Format(Net, "0.00;(0.00)")
        NetDisp = NetDisp.PadLeft(10, " ")
        If Net < 0 Then NetDisp = " " & NetDisp
        lbJournal.Items.Add(LedgerId & NetDisp)
    Loop
    If NetCounter = 0 Then
        lblAdvice.ForeColor = Color.DarkRed
        lblAdvice.Text = "There are no details posted for " & cbJVDate.Text & "..."
    ElseIf JVNet = 0 Then
        lblAdvice.ForeColor = Color.Black
        lblAdvice.Text = "The journal for this date nets zero."
    Else
        lblAdvice.ForeColor = Color.DarkRed
        lblAdvice.Text = "The journal for this date does not net zero." & vbCrLf & _
           vbCrLf & "It nets " & Format(JVNet, "0.00")
    End If
    Cnxn.Close()
End Sub

感谢您的帮助!

【问题讨论】:

  • 为了澄清,您使用的是 SQL CE 对吗?如果是这样,SQL CE 不支持 IFNULL 或 ISNULL 函数。
  • 是的,我们是。感谢您为我澄清这一点!

标签: vb.net visual-studio-2012


【解决方案1】:

在 VB 代码中,数据库 NULL 由DBNull.Value 表示。如果您从数据库中获取数据,那么您可能会执行以下操作:

Dim str As String = Nothing
Dim int As Integer? = Nothing

If Not myDataRow.IsNull("Text") Then
    str = CStr(myDataRow("Text"))
End If

If Not myDataRow.IsNull("Number") Then
    int = CInt(myDataRow("Number"))
End If

反过来,你可能会这样做:

Dim command As New SqlCommand("UPDATE MyTable SET Text = @Text, Number = @Number WHERE ID = @ID", connection)

command.Parameters.Add("@Text", SqlDbType.VarChar, 50).Value = If(str Is Nothing, CObj(DBNull.Value), str)
command.Parameters.Add("@Number", SqlDbType.Int).Value = If(int.HasValue, int.Value, CObj(DBNull.Value))

【讨论】:

    【解决方案2】:

    SQL CE 不支持 IFNULL 或 ISNULL 函数,要复制该函数,一种方法是使用 CASE..END 构造:

    SQLStmt = "SELECT LedgerId, 
    CASE SUM(Qty * Each) IS NULL THEN 0 END AS Net
    FROM Orders, Details
    WHERE CONVERT(nvarchar(10), AcctgDate, 101) = '" & cbJVDate.Text & "' AND Orders.Id = Details.OrderId
    GROUP BY LedgerId ORDER BY LedgerId"
    

    另一种方法是使用 COALESCE(我无法测试,因为我没有 SQL CE)

    SQLStmt = "SELECT LedgerId, 
    CAOLESCE(SUM(Qty * Each),0) AS Net
    FROM Orders, Details
    WHERE CONVERT(nvarchar(10), AcctgDate, 101) = '" & cbJVDate.Text & "' AND Orders.Id = Details.OrderId
    GROUP BY LedgerId ORDER BY LedgerId"
    

    【讨论】:

    • 非常感谢!我替换了 SQL 语句,现在它返回:“Execute JVReader with 'SELECT LedgerId, IFNULL(sum(Qty*Each),0) as Net FROM Orders, Details WHERE CONVERT(nvarchar(10), AcctgDate, 101) = '07 /29*=/2014' ANDOrders.Id = Details.OrderId " GROUP BY LedgerId ORDER BY LedgerId' got 解析查询时出错。 [令牌行号 = 1,令牌行偏移量 = 131,错误令牌 = ANDOrders]
    • 请再次检查查询中有一些错字
    • 抱歉,我无法编辑我之前的评论。这是编辑:编辑:我修复了一些语法错误,现在它说:[令牌行号=1,令牌行偏移=117,错误令牌=2014年7月29日]这对我来说没有意义因为那是从数据库来的。 (非常感谢您的帮助)
    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2023-03-07
    • 2015-06-07
    • 2012-11-19
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多