[英]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
更改
tbPatientID
到tbPatientID.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.