0

我不知道为什么以下查询没有使用我预期的参数执行!

cmdTxt.Append("UPDATE sd32depart SET currentcredit = currentcredit + ? WHERE year = ? AND main_code = ? ");
paramList.Add("currentcredit", value.ToString().TrimEnd());
paramList.Add("year", year.ToString().TrimEnd());
paramList.Add("main_code", main_code.ToString().TrimEnd());
res = ConnectionObj.Execute_NonQueryWithTransaction(cmdTxt.ToString(), CommandType.Text, paramList);

我明白了

res = 1虽然currentcredit = 180作为参数

当我检查我的桌子时,我发现了currentcredit NULL


public int Execute_NonQueryWithTransaction(string cmdText)
            {
                string return_msg = "";
                int return_val = -1;
                //check if connection closed then return -1;
                if (connectionstate == ConnectionState.Closed)
                    return -1;
                command.CommandText = cmdText;
                command.CommandType = CommandType.Text;
                command.Transaction = current_trans;
                try
                {
                    return_val = command.ExecuteNonQuery();
                }
                catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
                {
                    return_val = ifxEx.Errors[0].NativeError;
                    return_msg = return_val.ToString();
                }
                catch (Exception ex)// Handle all other exceptions.
                {
                    return_msg = ex.Message;
                }
                finally
                {
                    if (!string.IsNullOrEmpty(return_msg))//catch error
                    {
                        //rollback
                        current_trans.Rollback();
                        Close_Connection();
                    }

                }
                return return_val;
            }
4

1 回答 1

1

从评论:

currentcredit 在更新前为空,我该怎么办

啊,那就是问题所在了。在 SQL 中,null是粘性的。null+ 任何东西都是:null. 如果这是 TSQL(即 SQL Server),则解决方案是ISNULL

UPDATE sd32depart SET currentcredit = ISNULL(currentcredit,0) + ?

其中if的结果为非空,否则ISNULL(x, y)为。在 C# 术语中,它相当于(实际上,与 相同,除了它是varadic)。xxyx ?? yISNULL(x, y)COALESCE(x, y)COALESCE

所以:找到与ISNULLor等​​效的informix COALESCE,并使用它

通过简短的搜索,似乎在 informix中该NVL函数是这样做的,所以尝试:

UPDATE sd32depart SET currentcredit = NVL(currentcredit,0) + ?
于 2013-10-10T09:40:25.227 回答