开发者

Convert OracleParameter.Value to Int32

开发者 https://www.devze.com 2023-01-03 00:11 出处:网络
I have a stored procedure call that goes like this: using (OracleConnection con = new OracleConnection(ConfigurationManager.AppSettings[\"Database\"]))

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' t开发者_如何转开发o type 'System.IConvertible'."

Any thoughts? Thanks.


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


What about

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


It is more efficient to use

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


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);
    }
}


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消