![](/img/trans.png)
[英]How to extract data from Access db and place it into a text box using vb.net?
[英]How to write Date to Access DB from VB.net
我在將日期寫入數據庫時遇到麻煩。 我一直到處尋找,看到了許多相互矛盾的標准。
基本上,我總是遇到類型不匹配的情況。 我敢肯定,這是我沒看到的簡單事情。
我的代碼如下。 基本上,我進行查詢,添加參數,然后嘗試運行它。 名稱和電話號碼是文本,訂購的產品應該是數字(更具體地說是產品ID),日期是日期。
為什么在運行命令時出現不匹配?
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
數據類型不匹配的原因是參數名稱周圍出現了單引號。基本上,您傳遞的是字符串而不是數字或日期
sqlCMD = "" + _
"INSERT INTO Orders " + _
" ([NAME], " + _
" [PhoneNumber], " + _
" [ProductOrdered], " + _
" [OrderDate]) " + _
"VALUES (@CustomerName, " + _
" @PhoneNumber, " + _
" @ProductOrdered, " + _
" @OrderDate); "
這可能是類型不匹配的真正原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.