简体   繁体   English

为什么我的sql命令给我一个错误的数据类型错误

[英]Why is my sql command giving me a wrong data type error

This is my code 这是我的代码

 OleDbCommand cmd = new OleDbCommand("UPDATE Patient SET FirstName ='" + tbPatientFirstName.Text + "' WHERE PatientID='" + tbPatientID+ "' ",conn);

The text I'm an changing in the tbPatientFirstName is from a string to a string. 我在tbPatientFirstName中更改的文本是从字符串到字符串。 The PatientID is the records primary key. PatientID是记录的主键。 I know I'm not using parameterised sql but its not required and I don't have time to learn it at the moment 我知道我没有使用参数化的sql,但不是必需的,目前我没有时间学习它

Apart from the suggestion to use paramterized query (which you believe is not needed, so hopefully it is OK), three things seem to be the potential causes of the error. 除了建议使用paramterized query (您认为不需要该paramterized query ,因此希望可以)之外,似乎还有三点可能是导致错误的原因。

First, you seem to use the TextBox object rather than its Text . 首先,您似乎使用的是TextBox对象,而不是其Text Change tbPatientID to tbPatientID.Text 更改tbPatientIDtbPatientID.Text

Secondly, if your PatientID is integer, you may need to remove the apostrophe ' for the tbPatientID 其次,如果您的PatientID为整数,则可能需要删除tbPatientID的撇号'

WHERE PatientID=" + tbPatientID.Text.Trim()+ " " //no apostrophe

Thirdly, to avoid having whitespace undetected, consider using Trim() : 第三,为避免检测到whitespace ,请考虑使用Trim()

OleDbCommand cmd = new OleDbCommand("UPDATE Patient SET FirstName ='" + tbPatientFirstName.Text.Trim() + "' WHERE PatientID=" + tbPatientID.Text.Trim()+ " ",conn);

The time required to use a parameterized query is a lot less than the time required to fix these errors 使用参数化查询所需的时间比修复这些错误所需的时间少得多

OleDbCommand cmd = new OleDbCommand(@"UPDATE Patient 
       SET FirstName =@name WHERE PatientID= @id", conn);

cmd.Parameters.Add("@name", OleDbType.VarWChar).Value = tbPatientFirstName.Text 
cmd.Parameters.Add("@id", OleDbType.Integer).Value = Convert.ToInt32(tbPatientID.Text);

And that's all is required. 这就是全部。 Now you don't have troubles in passing values to your database engine also if these values contains a single quote. 现在,即使这些值包含单引号,也无需麻烦将值传递到数据库引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的方法名称给我一个错误? - Why is my method name giving me an error? 为什么我的委托程序给我一个错误? - Why my delegate programs giving me an error? 此代码段有什么问题? 它给我类型声明错误 - What is wrong with this code snippet? Its giving me type declaration error 为什么我的float变量语句不断显示“无法将类型double隐式转换为float”错误? - why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error? 为什么我的LINQ查询给我旧数据? - Why is my LINQ query giving me old data? 为什么我的int值在更改时给我一个错误? - Why is my int value giving me a error when changing it? 为什么我的反序列化给我Newtonsoft.Json.JsonConvert一个错误? - Why my deserialization is giving me an error Newtonsoft.Json.JsonConvert? 为什么我的网络服务给我一个错误500? - Why is my web service giving me an Error 500? 为什么我的标签字段在此C#代码中给我错误 - Why is my label field is giving me an error in this c# code 为什么如果使用逻辑条件运算符&&和!=的语句给我错误的结果? - Why If statements using logical conditional operators && and != are giving me the wrong result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM