Access中的数据类型不匹配错误 [英] Data type mismatch error in Access

查看:131
本文介绍了Access中的数据类型不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码用于根据序列号对访问数据中的更新数据进行编码.
在数据库中,它为整数数据类型.通过文本框输入序列号.它是字符串数据类型.我将其转换为整数数据类型,然后检查查询字符串中的where条件.请帮忙.



This is coding for update data in access databese based on serial number.
In database it in integer datatype. Enter the serial number through textbox. It is in string datatype. I converted into integer data type and then check the where conditon in query string. Please do some help.



protected void Button11_Click(object sender, EventArgs e)
   {
     int x;
     String s;
     s = TextBox1.Text;
     x=(Convert.ToInt32(s));
    cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno='" + s +"'", con);
    cmd.ExecuteNonQuery();



错误:条件表达式中的数据类型不匹配.



谢谢您,



Error:pre>Data type mismatch in criteria expression.



Thank you,

推荐答案

请不要那样做.这是对数据库的意外或故意破坏的邀请.
请改用参数化查询(您的问题也会消失).
Please, don''t do it that way. That is an invitation to the accidental or deliberate destruction of your database.
Use Parametrised queries instead (and your problem will disapear as well).
cmd = new OleDbCommand("UPDATE userdata SET firstname=@FN, lastname=@LN, department=@DP, software=@SW, hardware=@HW WHERE sno=@SN", con);
cmd.Parameters.AddWithValue("@FN", TextBox2.Text);
cmd.Parameters.AddWithValue("@LN", TextBox7.Text);
cmd.Parameters.AddWithValue("@DP", TextBox3.Text);
cmd.Parameters.AddWithValue("@SW", TextBox4.Text);
cmd.Parameters.AddWithValue("@HW", TextBox5.Text);
cmd.Parameters.AddWithValue("@SN", x);
cmd.ExecuteNonQuery()



哦,顺便说一句:不要为控件使用默认名称.您可能还记得TextBox5拥有今天的硬件,但是六周后呢?没有机会.如果改为使用tbHardware,则在编写它以及回头进行维护时很明显.



Oh, and BTW: don''t use the default name for controls. You may remember that TextBox5 has the Hardware today, but in six weeks time? No chance. If you call it tbHardware instead, then it is obvious when you write it, and when you look back on it for maintenance.


尝试一下此

Try this

cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno=" + x +"", con);



但是我建议您使用参数化查询来避免SQL注入攻击.

希望对您有所帮助:)



But I recommend you to use parametrized query to avoid SQL injection attacks.

hope it helps :)


你好朋友

您正在将TextBox1.Text值转换为名为"x"的int变量,然后将字符串值"s"传递给查询...,还将sno强制转换为",因此它成为字符串而不是int,
因此,请尝试使用此查询代替您的查询


Hello Friend

your are converting TextBox1.Text value to int variable named "x", and you pass your string value "s" to your query... and also cast your sno in '' '' so its become a string instead of int,
so try this query instead of yours


protected void Button11_Click(object sender, EventArgs e)
{
     int x;
     string s;
     s = TextBox1.Text;
     x=(Convert.ToInt32(s));
     cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno=" + x, con);
}
 cmd.ExecuteNonQuery();


这篇关于Access中的数据类型不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆