将 Byte() 转换为 Date() 时出现问题

发布于 2024-11-16 14:54:17 字数 836 浏览 2 评论 0原文

我的 DAL 中有一个查询,结果为 1 项,即日期。它要么是日期,要么为空。但是当值为空时我会收到错误。

从“DBNull”类型到“Date”类型的转换无效。

查询

Public Function GetOrderDepositByOrderID(ByVal OrderID As Integer) As Date
    myconn.Open()

    Dim date As Date

    Dim sql As String = "SELECT ifnull(OrderDeposit, '1900-01-01') FROM Order WHERE OrderID = ?"
    Dim cmd As New OdbcCommand(sql, myconn)

    cmd.Parameters.AddWithValue("OrderID", OrderID)

    date= cmd.ExecuteScalar()

    'connectie sluiten
    myconn.Close()

    Return date
End Function

这就是我调用函数的方式。

If bllCust.getOrderDepositByOrderID(OrderID) = DBNull Then
   lblBoodschap.Text = ("Deposit not paid.\n")
Else
   lblBoodschap.Text = ("Deposit paid.\n")
End If

如果支付了押金,表中就会有一个日期,如果没有支付,则为空。

欢迎所有帮助!

I have a query in my DAL that results 1 item, a date. It's either a date or null. But I get an error when the value is null.

Conversion from type 'DBNull' to type 'Date' is not valid.

Query

Public Function GetOrderDepositByOrderID(ByVal OrderID As Integer) As Date
    myconn.Open()

    Dim date As Date

    Dim sql As String = "SELECT ifnull(OrderDeposit, '1900-01-01') FROM Order WHERE OrderID = ?"
    Dim cmd As New OdbcCommand(sql, myconn)

    cmd.Parameters.AddWithValue("OrderID", OrderID)

    date= cmd.ExecuteScalar()

    'connectie sluiten
    myconn.Close()

    Return date
End Function

This is how I call my function.

If bllCust.getOrderDepositByOrderID(OrderID) = DBNull Then
   lblBoodschap.Text = ("Deposit not paid.\n")
Else
   lblBoodschap.Text = ("Deposit paid.\n")
End If

If the deposit is paid there's a date in the table, if not it's left null.

All help is welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心激荡 2024-11-23 14:54:17
  • 尝试使用 Convert.IsDBNull()
  • 考虑返回来自 getOrderDepositByOrderID()Nullable 来检查 null,而不是

C# 中的 DBNull 你的代码应该是这样的:

return date = cmd.ExecuteScalar() as DateTime?;

我不了解 VB.NET 中的运算符模拟

或者

If Convert.IsDBNull(bllCust.getOrderDepositByOrderID(OrderID)) Then
    ...
Else
    ...
Else If
  • Try to use Convert.IsDBNull()
  • Consider to return Nullable<DateTime> from getOrderDepositByOrderID() to check against null, not DBNull

in C# your code should like this:

return date = cmd.ExecuteScalar() as DateTime?;

I don't know operator as analog in VB.NET

or

If Convert.IsDBNull(bllCust.getOrderDepositByOrderID(OrderID)) Then
    ...
Else
    ...
Else If
旧瑾黎汐 2024-11-23 14:54:17

使用操作数 IS 而不是 =

If bllCust.getOrderDepositByOrderID(OrderID) Is DBNull Then
   lblBoodschap.Text = ("Deposit not paid.\n")
Else
   lblBoodschap.Text = ("Deposit paid.\n")
End If

Use the operand IS instead of =

If bllCust.getOrderDepositByOrderID(OrderID) Is DBNull Then
   lblBoodschap.Text = ("Deposit not paid.\n")
Else
   lblBoodschap.Text = ("Deposit paid.\n")
End If
望她远 2024-11-23 14:54:17

编辑数据库字段,使空值获得默认值 1900-01-01,这是一个永远不会使用的日期。

Edited the database fields so that null values get a default value 1900-01-01, a date that will never be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文