更新在VB.net中具有短文本和数字类型字段的MsAccess数据库

时间:2015-06-04 09:35:17

标签: vb.net oledb

我有Textboxes,我想使用这些database更新我的Textboxes。我试过删除' '我RegularOTQuery数据库字段中的符号,但我仍然 当我按下Button时,不断收到此错误:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Data type mismatch in criteria expression.

注意:RegularOT字段在数据库中都是Number类型,而evertything是Short Text

这是我的OLEdb声明:

"UPDATE dataNA SET today = '" & datestring & "', 
ProjectCode = '" & PrjctCodePassLbl.Text & "', 
Project = '" & PrjctNameTxtBox.Text & "', 
Mark = '" & MarkTxtBox.Text & "', 
Activity = '" & ActvtyTxtBox.Text & "', 
Regular = '" & Integer.Parse(RglrTxtBox.Text) & "', 
OT = '" & Integer.Parse(OTTxtBox.Text) & "' 

WHERE today = '" & DateLbl.Text & "' AND ProjectCode = '" & PrjctCodePassLbl.Text & "' 
AND Project = '" & PrjctNameLbl.Text & "' 
AND Mark = '" & MarkLbl.Text & "' 
AND Activity = '" & ActvtyLbl.Text & "' 
AND Regular = '" & RglrLbl.Text & "' 
AND OT = '" & OTLbl.Text & "' "

2 个答案:

答案 0 :(得分:0)

您应该使用参数化查询来指定添加到查询中的每个参数的数据类型。通过这种方式,数据库引擎确切地知道如何处理您的值,并在需要时使用适当的引号将它们分配给您的查询。

我会给你一个例子(不完整,但这应该给你一个有效的起点)

Dim sql = "UPDATE dataNA SET today = @dt, " & _
          "ProjectCode = @pcode, " & _
          "Project = @prj, " & _
          "Mark = @mark, " & _
          "Activity = @activity, " & _
          "Regular = @regular, " & _
          "OT = @ot " & _
          "WHERE today = @dt1 AND ProjectCode = @pcode1 " & _
          "AND Project = @prj1 "&  
          "AND Mark = @mark1 " & _
          "AND Activity = @activity1 " & _
          "AND Regular = @regular1 " & _
          "AND OT = @ot1 "
 Using cnn = new OleDbConnection(... connectionstring here....)
 Using cmd = new OleDbCommand(sql, cnn)
     cnn.Open()
     With cmd
         .Parameters.Add("@dt", OleDbType.VarWChar).Value = datestring 
         .Parameters.Add("@pcode", OleDbType.VarWChar).Value = PrjctCodePassLbl.Text 
         ... and so on with all the other parameters...
         ... remember that with OleDb you need to add them 
         ... to the parameters collection in the same EXACT order 
         ... in which they appear in the sql string.
         ... Of course every parameter should have the correct 
         ... OleDbType expected by your table, for example
         .Parameters.Add("@regular", OleDbType.Integer).Value = Integer.Parse(RglrTxtBox.Text)
         ... finally call
     End With
     cmd.ExecuteNonQuery
 End Using
 End Using

检查OleDbType enum here

可能是错误是由更新和WHERE子句中的两个整数值的值引用。无论如何,参数化查询可以避免这些不幸。

答案 1 :(得分:0)

我删除了常规和加密的UPDATEWHERE上的''(单引号)并且有效

"UPDATE dataNA SET today = '" & datestring & "', 
ProjectCode = '" & PrjctCodePassLbl.Text & "', 
Project = '" & PrjctNameTxtBox.Text & "', 
Mark = '" & MarkTxtBox.Text & "', 
Activity = '" & ActvtyTxtBox.Text & "', 
Regular = " & Integer.Parse(RglrTxtBox.Text) & ", 
OT = " & Integer.Parse(OTTxtBox.Text) & " 
WHERE today = '" & DateLbl.Text & "' AND 
ProjectCode = '" & PrjctCodePassLbl.Text & "' AND 
Project = '" & PrjctNameLbl.Text & "' AND 
Mark = '" & MarkLbl.Text & "' AND 
Activity = '" & ActvtyLbl.Text & "' AND 
Regular = " & Integer.Parse(RglrLbl.Text) & " AND 
OT = " & Integer.Parse(OTLbl.Text) & " "