Convert OracleParameter.Value to Int32

旧巷老猫 提交于 2019-12-17 15:49:07

问题


I have a stored procedure call that goes like this:

using (OracleConnection con = new OracleConnection(ConfigurationManager.AppSettings["Database"]))
using (OracleCommand cmd = new OracleCommand("Package.Procedure", con))
{
   Int32 existsCount;

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("successCount", OracleDbType.Int32, 0, ParameterDirection.InputOutput);
   cmd.Parameters.Add("BusinessId", OracleDbType.Int64, listRec.BusinessId, ParameterDirection.Input);

    con.Open();
    cmd.ExecuteScalar();
    con.Close();

    existsCount = Convert.ToInt32(cmd.Parameters["successCount"].Value);

    return (existsCount);
}

But on this line:

existsCount = Convert.ToInt32(cmd.Parameters["successCount"].Value);

It throws the Exception "Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal' to type 'System.IConvertible'."

Any thoughts? Thanks.


回答1:


What about

existsCount = int.Parse(cmd.Parameters["successCount"].Value.ToString());



回答2:


You can also try:

Oracle.DataAccess.Types.OracleDecimal d = (Oracle.DataAccess.Types.OracleDecimal)cmd.Parameters["successCount"].Value;

if( d.IsNull )
    existsCount = 0;
else
    existsCount = d.ToInt32( );



回答3:


It is more efficient to use

Convert.ToInt32((decimal)(OracleDecimal)(cmd.Parameters["successCount"].Value))



回答4:


I don't know the return type at runtime because the execution code is within a cross-platform data access framework under development, so I used a switch on the parameter value type to access the underlying Oracle[type].Value property for the various Oracle managed data access types.

public override object GetValue(IDataParameter parameter)
{
    if (parameter == null)
    {
        throw new ArgumentNullException(nameof(parameter));
    }

    // https://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleDbTypeEnumerationType.htm
    if (parameter is OracleParameter)
    {
        switch (parameter.Value)
        {
            case OracleBinary oracleBinary:
                // returns byte[]
                return oracleBinary.Value;
            case OracleBoolean oracleBoolean:
                // returns bool
                return oracleBoolean.Value;
            case OracleDate oracleDate:
                // returns DateTime
                return oracleDate.Value;
            case OracleDecimal oracleDecimal:
                // oracleDecimal.Value is Decimal, so we convert to correct type.
                return parameter.DbType == DbType.Decimal
                    ? oracleDecimal.Value
                    : Convert.ChangeType(oracleDecimal.Value, parameter.DbType.ToType());
            case OracleIntervalDS oracleIntervalDS:
                // returns TimeSpan
                return oracleIntervalDS.Value;
            case OracleIntervalYM oracleIntervalYM:
                // returns Long
                return oracleIntervalYM.Value;
            case OracleTimeStamp oracleTimeStamp:
                // returns DateTime
                return oracleTimeStamp.Value;
            case OracleTimeStampLTZ oracleTimeStampLTZ:
                // returns DateTime
                return oracleTimeStampLTZ.Value;
            case OracleTimeStampTZ oracleTimeStampTZ:
                // returns DateTime
                return oracleTimeStampTZ.Value;
            default:
                throw new NotSupportedException(
                    parameter.Value != null
                        ? parameter.Value.GetType().Name
                        : parameter.ParameterName);
        }
    }
    else
    {
        throw new NotSupportedException(parameter.GetType().Name);
    }
}



回答5:


In my case, I'm using Bulk Insert in Oracle, and meet the same error, let me share my solution here. I solved it by adding

oracleCommand.ArrayBindCount = datas.Count; 

that is I forgot to set the ArrayBindCount property.




回答6:


I suggest you convert to String, and after that you convert from String to Integer.

Dim tmpIdSesiónCalificación As String = 
    parametroIdSesiónCalificación.Value.ToString
_idSesiónCalificación = Convert.ToInt32(tmpIdSesiónCalificación)


来源:https://stackoverflow.com/questions/3026036/convert-oracleparameter-value-to-int32

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!