如下面所示的代码,我想从OracleParameter对象中获取值.它的数据类型是datetime.
... Dim cmd As New OracleCommand("stored_proc_name", cnObject) cmd.Parameters.Add("tran_date_out", OracleDbType.Date, ParameterDirection.Output) ... cmd.ExecuteNonQuery() ... Dim tranDate As Date tranDate = cmd.Parameters("tran_date_out").Value
当我为tranDate变量赋值时,我收到一个错误.但如果我编码如下,我只得到日期.
tranDate = CDate(cmd.Parameters("tran_date_out").Value.ToString)
那么如何才能获得tranDate变量的日期和时间值?
脱离我的头脑,当一个out参数被分配给一个奇怪的Oracle盒装类型时,OracleParameter.Value.它似乎是一个完全糟糕的设计或Oracle的一部分......但不是返回String,而是获得OracleString等.
每个Oracle类型都有一个.value的具有系统类型,当然他们不都实现一个共同的接口暴露这一点,所以我所做的就是基本上写一个方法拆箱的类型:
////// The need for this method is highly annoying. /// When Oracle sets its output parameters, the OracleParameter.Value property /// is set to an internal Oracle type, not its equivelant System type. /// For example, strings are returned as OracleString, DBNull is returned /// as OracleNull, blobs are returned as OracleBinary, etc... /// So these Oracle types need unboxed back to their normal system types. /// /// Oracle type to unbox. ///internal static object UnBoxOracleType(object oracleType) { if (oracleType == null) return null; Type T = oracleType.GetType(); if (T == typeof(OracleString)) { if (((OracleString)oracleType).IsNull) return null; return ((OracleString)oracleType).Value; } else if (T == typeof(OracleDecimal)) { if (((OracleDecimal)oracleType).IsNull) return null; return ((OracleDecimal)oracleType).Value; } else if (T == typeof(OracleBinary)) { if (((OracleBinary)oracleType).IsNull) return null; return ((OracleBinary)oracleType).Value; } else if (T == typeof(OracleBlob)) { if (((OracleBlob)oracleType).IsNull) return null; return ((OracleBlob)oracleType).Value; } else if (T == typeof(OracleDate)) { if (((OracleDate)oracleType).IsNull) return null; return ((OracleDate)oracleType).Value; } else if (T == typeof(OracleTimeStamp)) { if (((OracleTimeStamp)oracleType).IsNull) return null; return ((OracleTimeStamp)oracleType).Value; } else // not sure how to handle these. return oracleType; }
这可能不是最干净的解决方案,但...这是快速和肮脏的,一个第二确实为我工作.
只需将OracleParameter.Value传递给此方法即可.
实际上,在回答之前,我可能只有1/2读过您的问题.我认为Oracle的Date类型只包含日期而不是时间.
oracle类型Timestamp既有日期又有时间.
希望有所帮助!:)