数字转换过程中的字符失败

时间:2012-06-14 09:11:50

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

当我尝试将参数化查询与informix一起使用时,我遇到以下异常。

A character to numeric conversion process failed

                    StringBuilder sQuery = new StringBuilder();
                    Dictionary<string, string> paramList = new Dictionary<string, string>();
                    cmdTxt.Append(" UPDATE userwidgetto SET widget_color = ?, widget_column = ?, widget_order = ?,widget_state = ?,widget_type = ? ");
                    cmdTxt.Append(" WHERE process_id = ? AND emp_num = ? ");

                    paramList.Add("process_id", Process_id.ToString());
                    paramList.Add("emp_num", Emp_num.ToString());
                    paramList.Add("widget_color", Widget_color.TrimEnd());
                    paramList.Add("widget_column", Widget_column.ToString().TrimEnd());
                    paramList.Add("widget_order", Widget_order.ToString());
                    paramList.Add("widget_state", Widget_state.ToString());
                    paramList.Add("widget_type", Widget_type.ToString());
                    affectedRow = DAL_Helper.Execute_NonQuery(cmdTxt.ToString(), CommandType.Text, paramList);

public int Execute_NonQuery(string cmdText, CommandType cmdType, Dictionary<string, string> Param_arr)
            {
                Open_Connection();
                int return_val = -1;
                command.CommandText = cmdText;
                command.CommandType = cmdType;
                command.Transaction = current_trans;
                command.Parameters.Clear();
                if (Param_arr != null)
                {
                    foreach (KeyValuePair<string, string> parameter in Param_arr)
                    {
                        param = command.CreateParameter();
                        param.ParameterName = parameter.Key.ToString();
                        if (parameter.Value.ToString() == "Null" || string.IsNullOrEmpty(parameter.Value))
                            param.Value = DBNull.Value;
                        else
                            param.Value = parameter.Value.ToString();
                        command.Parameters.Add(param);
                    }
                }
                try
                {
                    return_val = command.ExecuteNonQuery();//means no error message //OK                
                }
                catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
                {
                    return_val = ifxEx.Errors[0].NativeError;
                }
                catch (Exception Ex)
                {
                    ErrMapping.WriteLog("\r\n" + Ex.Message);
                    return_val = ExCodeConst;
                }
                finally
                {
                    Close_Connection();
                }
                return return_val;
            }

1 个答案:

答案 0 :(得分:3)

我想您正在使用OleDb客户端。 在这种情况下,参数应按照命令字符串占位符(?)的相同位置顺序插入

cmdTxt.Append(" UPDATE userwidgetto SET widget_color = ?, widget_column = ?, widget_order = ?,widget_state = ?,widget_type = ? ");
cmdTxt.Append(" WHERE process_id = ? AND emp_num = ? ");
paramList.Add("widget_color", Widget_color.TrimEnd());
paramList.Add("widget_column", Widget_column.ToString().TrimEnd());
paramList.Add("widget_order", Widget_order.ToString());
paramList.Add("widget_state", Widget_state.ToString());
paramList.Add("widget_type", Widget_type.ToString()); 
paramList.Add("process_id", Process_id.ToString());
paramList.Add("emp_num", Emp_num.ToString());
affectedRow = DAL_Helper.Execute_NonQuery(cmdTxt.ToString(), CommandType.Text, paramList); 

字典&lt; string,string&gt; paramList强制传递给DAL_Helper的每个值都是字符串类型 而且,我认为,从给定的参数名称来看并非如此( process_id 不是数据库表中的字符串对吗?)。
作为评论中已经建议的人,您应该将paramList更改为Dictionary&lt; string,object&gt;然后在适当的位置添加带字符串转换的参数(意思是,表列的数据类型是字符串类型)。