我在向数据库写日期时遇到了麻烦。我一直在寻找各地,看到许多相互矛盾的标准。
基本上我一直遇到类型不匹配。我确信这是我看不到的简单事。
我的代码如下。基本上,我进行查询,添加参数,并尝试运行它。 名称和语音是文本,产品订购应该是数字(更具体地说是productID),日期是日期。
为什么命令运行时会出现不匹配的情况?
Dim sqlCMD
sqlCMD = "" + _
"INSERT INTO Orders " + _
" ([NAME], " + _
" [PhoneNumber], " + _
" [ProductOrdered], " + _
" [OrderDate]) " + _
"VALUES ('@CustomerName', " + _
" '@PhoneNumber', " + _
" '@ProductOrdered', " + _
" '@OrderDate'); "
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection()
connection.ConnectionString = ConnectionStringConst
Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlCMD, connection)
command.Parameters.Add("@CustomerName", OleDb.OleDbType.Char).Value = txtOrderCustName.Text
command.Parameters.Add("@PhoneNumber", OleDb.OleDbType.Char).Value = txtOrderPhoneNum.Text
command.Parameters.Add("@ProductOrdered", OleDb.OleDbType.Integer).Value = cmbOrderItem.SelectedIndex
command.Parameters.Add("@OrderDate", OleDb.OleDbType.DBDate).Value = DateTime.Now
Try
connection.Open()
command.ExecuteNonQuery() 'Perform our query and insert into the table
Catch ex As Exception
MsgBox("ERROR - Your query may not have completed" + vbNewLine + vbNewLine + _
"Error Message: " + ex.Message)
Finally
connection.Close()
End Try
答案 0 :(得分:1)
数据类型不匹配的原因是参数名称周围存在单引号 基本上你传递的是字符串而不是数字或日期
sqlCMD = "" + _
"INSERT INTO Orders " + _
" ([NAME], " + _
" [PhoneNumber], " + _
" [ProductOrdered], " + _
" [OrderDate]) " + _
"VALUES (@CustomerName, " + _
" @PhoneNumber, " + _
" @ProductOrdered, " + _
" @OrderDate); "
这可能是您的类型不匹配的真正原因。