将NULL返回到dbtype.date的输出参数

时间:2019-05-20 09:42:16

标签: vb.net oracle odp.net

我有一个存储过程,该过程将日期或null返回到命令参数中。但是,我在处理null回报时遇到困难。

该过程如下所示:

PROCEDURE GetDate(inParam IN VARCHAR2, outDate OUT DATE) AS
    BEGIN
        SELECT MAX(DateVal) INTO outDate
        FROM Table1
        WHERE Col1 = inParam;
    EXCEPTION 
        WHEN no_data_found THEN
        outDate:= NULL;
END GetDate;

我的VB.NET代码如下:

Dim returnDate As Date

Connection.Open()
Command.CommandType = CommandType.StoredProcedure
Command.CommandText = "GEN_PACKAGE.GetDate"
Command.Parameters.Add("inParam", inParam).Direction = ParameterDirection.Input
Command.Parameters.Add("outDate", OracleDbType.Date).Direction = ParameterDirection.Output
Command.ExecuteNonQuery()
returnDate = CDate(Command.Parameters("outDate").Value)

但是,使用此方法,我在Conversion from type 'OracleDate' to type 'Date' is not valid.上获得了异常returnDate = CDate(Command.Parameters("outDate").Value)

如果我将OracleDbType.Date更改为DbType.Date(我从另一个答案中找到了解决方案),我将在Value does not fall within the expected range上得到Command.Parameters.Add("D0131Date", DbType.Date).Direction = ParameterDirection.Output

当我的返回日期为null或不为null时,如何为给定的输入返回日期?

2 个答案:

答案 0 :(得分:1)

对不起,无法测试。没有Oracle

    Dim returnDate = If(Command.Parameters("outDate").Value = DBNull.Value, Nothing, Command.Parameters("outDate").Value)
    If returnDate Is Nothing Then
        'Handle a null return
    Else
        Dim MyDate As Date = DirectCast(returnDate, Date)
    End If

答案 1 :(得分:0)

您可以将过程的异常块转换为

EXCEPTION 
        WHEN no_data_found THEN
        outDate:= date'1900-01-01';

返回一个非常旧的日期,并将此特殊值视为通过ifcase语句与outDate的值进行比较而在您的应用程序中过滤为空值