gpt4 book ai didi

c# - 字符到数字的转换过程失败

转载 作者:行者123 更新时间:2023-11-30 22:27:59 25 4
gpt4 key购买 nike

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

最佳答案

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

关于c# - 字符到数字的转换过程失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11029994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com