在我的脑海中,OracleParameter.Value,当一个 out 参数时,被分配给一个奇怪的 Oracle 盒装类型。这似乎是一个完全糟糕的设计或 Oracle 的部分......但不是返回 String,而是你会得到 OracleString,等等。
每个 Oracle 类型都有一个具有系统类型的 .Value,但当然它们并不都实现了一个公共接口来公开它,所以我所做的基本上是编写一个方法来拆箱类型:
/// <summary>
/// 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.
/// </summary>
/// <param name="oracleType">Oracle type to unbox.</param>
/// <returns></returns>
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 有日期和时间。
希望有帮助!:)