将OracleDbType.Date转换为Date无效

时间:2019-05-20 11:18:32

标签: vb.net oracle odp.net

我正在尝试从OracleCommand.Parameter执行以下操作:

存储过程:

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;
Dim returnDate as Date?

Using Connection = New OracleConnection(connectionString)
Using Command As OracleCommand = Connection.CreateCommand()

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()

If Not CType(Command.Parameters("outDate").Value, INullable).IsNull Then
    returnDate = CDate(Command.Parameters("outDate").Value)
End If

End Using
End Using

但是它会引发以下异常:

Exception thrown: 'System.InvalidCastException' in Microsoft.VisualBasic.dll

Additional information: Conversion from type 'OracleDate' to type 'Date' is not valid.

如果无法将OracleDbType.Date转换为.NET Date类型,那该怎么办?我如何才能将OracleDbType.Date用作.NET Date(或Date ?,我已经尝试过两者)从函数中传递出去?

1 个答案:

答案 0 :(得分:0)

好,所以这是这里最大的误解(我经常从ODP用法中看到)

Command.Parameters("outDate").Value

您只需检索CLR类型。实际上,您得到OracleDate-see this

因此检索该值的代码将是

dim dotNetVal as DateTime = Command.Parameters("outDate").Value.Value

要检查是否为NULL

if Command.Parameters("outDate").Value.IsNull then

是的,这个演员-

CDate(Command.Parameters("outDate").Value)

无效,因为您无法将OracleDate投射到DateTime