条件表达式错误中的数据类型不匹配 [英] Data type mismatch in criteria expression error

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

问题描述

i'm trying to insert data from vb.net form to access data base, i have this code but i have this error:Data type mismatch in criteria expression.

dname: text

dphone: text

idnum: number

dbank: number

dcity: text

rdate: date and time





我是什么尝试过:



Dim conn As New OleDbConnection

Dim sConnString As String

Dim cmd作为新的OleDbCommand

Dim sSQL As String = String.Empty



sConnString =provider = microsoft.ace.oledb.12.0;数据源= | datadirectory | \ delegate_manager.accdb;



conn =新OleDbConnection(sConnString)

conn.Open()

cmd.Connection = conn

cmd.CommandType = CommandType.Text



cmd.CommandText =插入代表( dname,dphone,idnum,dbank,dcity,rdate)VALUES(@ dname,@ dphone,@ idnum,@ dbank,@ dcity,@ rdate)

cmd.Parameters.AddWithValue(@ dname ,TextBox1.Text)

cmd.Parameters.AddWithValue(@ dphone, TextBox2.Text)

cmd.Parameters.AddWithValue(@ idnum,TextBox3.Text)

cmd.Parameters.AddWithValue(@ dbank,TextBox4.Text)

cmd.Parameters.AddWithValue(@ dcity,TextBox5.Text)

cmd.Parameters.AddWithValue(@ rdate,Convert.ToDateTime(DateTimePicker1.Value。日期))

cmd.ExecuteNonQuery()

con.Close()



What I have tried:

Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim sSQL As String = String.Empty

sConnString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\delegate_manager.accdb;"

conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text

cmd.CommandText = "Insert into delegate (dname,dphone,idnum,dbank,dcity,rdate) VALUES(@dname,@dphone,@idnum,@dbank,@dcity,@rdate)"
cmd.Parameters.AddWithValue("@dname", TextBox1.Text)
cmd.Parameters.AddWithValue("@dphone", TextBox2.Text)
cmd.Parameters.AddWithValue("@idnum", TextBox3.Text)
cmd.Parameters.AddWithValue("@dbank", TextBox4.Text)
cmd.Parameters.AddWithValue("@dcity", TextBox5.Text)
cmd.Parameters.AddWithValue("@rdate", Convert.ToDateTime(DateTimePicker1.Value.Date))
cmd.ExecuteNonQuery()
con.Close()

推荐答案

查看数据,并检查TextBox3和TextBox4中的内容 - 因为您将这些放入数字字段,最好使用TryParse检查它们是数字条目,然后传递转换后的值,而不是盲目传递字符串值。



并帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得今天的TextBox8是手机号码,但是当你需要时在三周时间内修改它,你会吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......
Look at the data, and examine what's in TextBox3 and TextBox4 - since you are putting those to number fields, it would be a good idea to use TryParse to check they are numeric entries and then pass the converted values if they are instead of blindly passing a string value.

And do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


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

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