Frage

I need to insert a ulong (i.e., UInt64) number into an Oracle database, but am running into an ArgumentException. I'm inserting it into a NUMBER column, which I have assured myself should be able to store any number from 0 to 2^64-1.

Here is a minimal example that will reproduce the error:

// First create the following table:
//
//     CREATE TABLE SampleTable (SampleColumn NUMBER)
//
using (var dbConnection = new OracleConnection("..."))
{
    ulong valueToInsert = 123;

    OracleCommand command = dbConnection.CreateCommand();
    command.CommandText = @"INSERT INTO SampleTable (SampleColumn) VALUES (:SampleColumn)";
    command.Parameters.Add("SampleColumn", valueToInsert);

    command.ExecuteNonQuery();
}

This code works just fine if valueToInsert is of any other integer type (including long), but when I try it with ulong, I get the following error:

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=Oracle.DataAccess
  StackTrace:
     at Oracle.DataAccess.Client.OracleParameter..ctor(String parameterName, Object obj)
     at Oracle.DataAccess.Client.OracleParameterCollection.Add(String name, Object val)

Why is this happening?

P.S. I have tried using both Oracle.DataAccess 2.112.1.2 and Oracle.ManagedDataAccess 4.212.1.0.

War es hilfreich?

Lösung

Different C# types are allowed to map to different OracleDbTypes; a good listing is shown in an answer to this question.

It turns out there is no OracleDbType.UInt64, though I can not explain why. Anyway, my problem can be solved by converting the argument to a decimal type before inserting:

command.Parameters.Add("SampleColumn", (decimal)(valueToInsert));

This allows it to become OracleDbType.Decimal, which any ulong number is convertible to.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top