简体   繁体   中英

ORA-01722: invalid number on INSERT in C# - Oracle 11g

I have this Oracle 11g table

CREATE TABLE "DBNAME"."CANDIDATES" 
(
    "ID" NUMBER(24,0), 
    "USRINS" VARCHAR2(30 CHAR), 
    "DATINS" DATE, 
    "USRUPD" VARCHAR2(30 CHAR), 
    "DATUPD" DATE, 
    "EXM_ID" NUMBER(24,0), 
    "TYPE" NUMBER(3,0), 
    "PSN_ID" NUMBER(24,0), 
    "KOD" NUMBER(20,0), 
    "STATUS" NUMBER(20,0), 
    "PRICINA" VARCHAR2(200 CHAR)
)

Now i have this command in C#

string insertIntoCandidates = "INSERT INTO CANDIDATES " &
  "(USRINS, DATINS, PSN_ID, KOD, STATUS, PRICINA) " &
  values ("
                     + ":usrins, "
                     + ":datins, "
                     + ":psn_id, "
                     + ":kod, "
                     + ":status, "
                     + ":pricina"
                     + ") ";

command.Parameters.Add(":usrins", null);
command.Parameters.Add(":datins", DateTime.Now);
command.Parameters.Add(":psn_id", getPsnIDByEMBG(result.embg));
command.Parameters.Add(":kod", result.kod_kandidat);
if (result.status)
{
    command.Parameters.Add(":status", 1);
}
else
{
    command.Parameters.Add(":status", 0);
}

command.Parameters.Add(":pricina", result.pricina);

int res = command.ExecuteNonQuery();

The columns for which I don't insert a value, can get null values. After executing the last line, I get am exception ORA-01722: invalid number. I tried looking for an answer, but without any luck. Could you help me out? Thanks

An ORA-01722 ("invalid number") error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number. So, check the parameters for correct data types.

And also see:

C# parameterized queries for Oracle - serious & dangerous bug!

and

Why am I getting an ORA-01722 (invalid number)?

Further to michaos's answer, also note that it doesn't matter what you name your parameters, they have to be added in the order in which they appear in the query. If not, then you can get misleading ORA-01722 (and other) errors. Yes this is a horrible bug!

add

command.BindByName=true;

apparently Oracle defaults to positional binding instead of name binding.

而不是null你必须使用DBNull.Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM