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

时间:2013-10-10 08:55:14

标签: c# asp.net ado.net informix parameterized-query

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

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

1 个答案:

答案 0 :(得分:1)

来自评论:

  在更新之前,

currentcredit为null,我应该怎么做

啊,那就是问题所在。在SQL中,null很粘。 null +任何内容都是:null。如果这是TSQL(即SQL Server),解决方案将是ISNULL

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

如果ISNULL(x, y)非空,则x的结果为x,否则为y。在C#术语中,它等同于x ?? y(事实上,ISNULL(x, y)COALESCE(x, y)相同,除了COALESCE varadic )。

所以:找到相当于ISNULLCOALESCE的informix,使用

通过简短的搜索,似乎在informix the NVL function中这样做,所以请尝试:

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