简体   繁体   中英

Inserting null value to integer column in DB2

I have tried it before in mysql DB and it is working fine but this time seems like the syntax is not applicable for DB2 and it giving me exception if the variable is empty:

Here is my sample snippet:

var num = "";
string insert_qry = "INSERT INTO myTable(myCol) VALUES (@num)";
DB2Command checkCommand = new DB2Command(insert_qry, myConnection);

checkCommand.Parameters.Add("@num ", num ?? null);
affectRowNum = checkCommand.ExecuteNonQuery();

If I assign value from the variable it works fine but it is empty it will return this exception error:

IBM.Data.DB2.Core.DB2Exception: 'External component has thrown an exception.'

Any suggestion/Comments TIA

Try DbNull.Value this worked for me.

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