将varchar值'FOT'转换为数据类型int时转换失败 [英] Conversion failed when converting the varchar value 'FOT' to data type int
问题描述
String start_cd;
String end_cd;
opencon();
SqlCommand res = new SqlCommand("SELECT ID,'" + start_cd + "','" + end_cd + "' FROM Train_route_up WHERE '" + start_cd + "'!=0 or '" + end_cd + "'!=0", con);
SqlDataAdapter sda_res = new SqlDataAdapter(res);
DataTable dt_res = new DataTable();
sda_res.Fill(dt_res);
listBox1.DataSource=dt_res;
listBox1.DisplayMember=" +start_cd+ ";
//Train_route_up= SQL table name
//FOT=start_cd value(nvarchar)
这是错误消息: 将varchar值'FOT'转换为数据类型int 时,转换失败.我要去哪里错了?
I get this as an error: Conversion failed when converting the varchar value 'FOT' to data type int. Where am I going wrong?
推荐答案
您的错误是SQL Server在无法将字符串转换为整数的情况下在数据库认为有必要的情况下产生的错误.
Your error is an error the SQL Server produces when a string cannot be converted to an integer, in a context where the database thinks this is necessary.
您在 where
子句中具有以下表达式:
You have these expressions in the where
clause:
'" + start_cd + "'!=0 or '" + end_cd + "'!=0
您正在将字符串(单引号)与整数(0)进行显式比较.这是其中一种情况.如果字符串不是数字格式,则会生成错误.
You are explicitly comparing a string (in single quotes) to an integer (0). This is one of those situations. This will generate the error if the string is not in a numeric format.
我不知道您想做什么,也许是以下其中之一:
I don't know what you want to do, perhaps one of the following:
与字符串'0'
'" + start_cd + "'!='0' or '" + end_cd + "'!='0'
比较为NULL
'" + start_cd + "' is not null or '" + end_cd + "' is not null
请参阅列名而不是字符串:
Refer to column names and not a string:
" + start_cd + "!=0 or " + end_cd + "!=0
这篇关于将varchar值'FOT'转换为数据类型int时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!