首页 > 解决方案 > 将 OracleDbType.Date 转换为 Date 无效

问题描述

我正在尝试从以下位置执行以下操作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

但是它会引发以下异常:

抛出异常:Microsoft.VisualBasic.dll 中的“System.InvalidCastException”附加信息:从“OracleDate”类型到“Date”类型的转换无效。

OracleDbType.Date如果它不能转换为 .NETDate类型有什么意义?我怎样才能把我的作为.NET (或日期?,我都试过了)OracleDbType.Date从我的函数中传递出去?Date

标签: vb.netoracleodp.net

解决方案


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

Command.Parameters("outDate").Value

您只需检索一个 CLR 类型。实际上,你得到OracleDate-看到这个

所以检索值的代码是

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

并检查你做的 NULL

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

是的,这个演员阵容——

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

无效,因为您无法OracleDate转换为DateTime


推荐阅读