varchar 值转换成数据类型 int 时失败的问题

a6765685 2015-03-21 05:15:54
private void button5_Click(object sender, EventArgs e)
{

if (comboBox2.Text == "")
{
MessageBox.Show("请输入查询条件", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//string sqlstr2 = "select nullahnum,goodsnum,goods,breed,broperson,brobran,spec,bronum,brotime,price,allprice,remark from tb_borrow where( goodsnum='"

+ textBox8.Text + "' or goods='" + textBox8.Text + "')and brotime between'" + dateTimePicker2.Value.Date.ToShortDateString() + "' and '" +

dateTimePicker3.Value.Date.ToShortDateString() + "' order by brotime";
string sqlstr2 = "select ID,传感器编号,测点编号,监测值,监测日期 from 数据采集 where ( 传感器编号='" + comboBox2.Text + "' or 测点编号='" +

comboBox2.Text + "'or ID='" + comboBox2.Text + "')";
DataSet ds3 = database.getSet(sqlstr2, "数据采集");
if (ds3.Tables[0].Rows.Count == 0)
{
MessageBox.Show("查询无结果", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
BinData();
}

else
{
dataGridView1.DataSource = ds3.Tables[0];
this.dataGridView1.ReadOnly = false;
for (int i = 0; i <= 3; i++)
{
if (i == 0)
{
this.dataGridView1.Columns[i].ReadOnly = true;
}
else
{
this.dataGridView1.Columns[i].ReadOnly = false;
}
}
}
}
}
以上代码是查询按钮的事件,点击后在下段代码出现“在将 varchar 值 'DB1-1' 转换成数据类型 int 时失败。”的错误
public DataSet getSet(string str_sqlStr, string s_Tanble)
{
SqlConnection sqlcon = this.getCon();
SqlDataAdapter sqlda = new SqlDataAdapter(str_sqlStr, sqlcon);
DataSet ds = new DataSet();
sqlda.Fill(ds, s_Tanble); 此行出现//在将 varchar 值 'DB1-1' 转换成数据类型 int 时失败。//
return ds;
}
...全文
1156 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Regan-lin 2015-03-24
  • 打赏
  • 举报
回复
DB1-1这种字符串怎么可能转成int
exception92 2015-03-23
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
str_sqlStr 语句的问题。
Fill 之前 在sql编译器中 执行str_sqlStr 看看能不能成功
exception92 2015-03-23
  • 打赏
  • 举报
回复
str_sqlStr 语句的问题。
江南小鱼 2015-03-23
  • 打赏
  • 举报
回复
如果你额where添加里面的编号数据库表字段都是int类型 那么,你在界面采集用户输入的时候,用正则限制之内输入整数 或者 进行查询之前,先校验一下输入的值是否是Int型,如果不是给出相应的提示,待输入合法后再请求数据库
程序猿老曾 2015-03-23
  • 打赏
  • 举报
回复
'DB1-1' 这种肯定转不了的!!
qq_23889229 2015-03-23
  • 打赏
  • 举报
回复
varchar类型如果是 像 ‘123’这样的值可以转换,但是像你这样的值有字符的‘DB1-1’,是转换不了为int类型的
SPFarmer 2015-03-22
  • 打赏
  • 举报
回复
你的where里面,要比较 传感器编号 or 测点编号 or ID是不是等于comboBox2.Text。问题就在这里,这三个column里有一个是int类型的,而你的value是 DB1-1,它不是int。所以就转化不了了啊。
宝_爸 2015-03-21
  • 打赏
  • 举报
回复
DB1-1是where的参数还是select的结果。 我才是参数,你传DB1-1进去,但是要比较的column的类型去恩施int.

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧