2

当我尝试将参数化查询与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;
            }
4

1 回答 1

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

Dictionary<string, string> paramList 也强制传递给 DAL_Helper 的每个值都是字符串类型。
而且,我认为,从给出的参数名称来看,情况并非如此(process_id不是数据库表中的字符串,对吧?)。
正如评论中已经建议的那样,您应该将 paramList 更改为 Dictionary<string, object> ,然后仅在适当的地方添加带有字符串转换的参数(意味着表列的数据类型是字符串类型) .

于 2012-06-14T09:15:40.247 回答