简体   繁体   English

为什么我的查询不使用我的输入参数更新表?

[英]Why my query doesn't update the table with my input parameters?

I don't know why the following query doesn't executed with my expected parameters !! 我不知道为什么下面的查询没有用我期望的参数执行!

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

I get 我懂了

res = 1 and although currentcredit = 180 as a parameter res = 1 ,尽管currentcredit = 180作为参数

when i check my table i found currentcredit NULL !! 当我检查我的表时,我发现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;
            }

From the comments: 从评论:

currentcredit is null before the update, what should i do 更新之前currentcredit为null,我该怎么办

Ah, that's the problem then. 嗯,那就是问题所在。 In SQL, null is sticky. 在SQL中, null是粘性的。 null + anything is: null . null +任何是: null If this was TSQL (ie SQL Server), the solution would be ISNULL : 如果这是TSQL(即SQL Server),则解决方案将为ISNULL

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

where the result of ISNULL(x, y) is x if x is non-null, otherwise y . 如果x不为null,则ISNULL(x, y)x ,否则为y In C# terms, it is the equivalent of x ?? y 用C#术语来说,它等效于x ?? y x ?? y (and indeed, ISNULL(x, y) is identical to COALESCE(x, y) , except that COALESCE is varadic ). x ?? y (实际上, ISNULL(x, y)COALESCE(x, y) ,只是COALESCEvaradic )。

So: find the informix equivalent of ISNULL or COALESCE , and use that . 因此:找到等效于ISNULLCOALESCE ,然后使用

From a brief search, it seems that in informix the NVL function does this, so try: 从简短的搜索来看,似乎在notifyix中NVL函数可以做到这一点,因此请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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