如何删除此错误=“条件表达式中的数据类型不匹配” [英] how to remove this error="data type mismatch in criteria expression"
本文介绍了如何删除此错误=“条件表达式中的数据类型不匹配”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Namount.Text = Nothing Then
MsgBox("insert data first")
Else
Dim str, str1 As String
str = "insert into customer values(" & Val(bill.Text) & ",'" & dtp.Value & "','" & cName.Text & "','" & Address.Text & "','" & paymenttype.Text & "'," & Val(phoneno.Text) & "," & Val(Tamount.Text) & "," & Val(Discount.Text) & "," & Val(Namount.Text) & ")"
cmd = New OleDbCommand(str, cnn)
dap = New OleDbDataAdapter(cmd)
ds = New DataSet
dap.Fill(ds)
Dim i As Integer
Dim Barcode, Item_name As String
Dim Qty, item_rate, i_amt As String
For i = 0 To DataGridView1.RowCount - 1
Barcode = DataGridView1.Rows(i).Cells(0).Value
Item_name = DataGridView1.Rows(i).Cells(1).Value
Qty = DataGridView1.Rows(i).Cells(2).Value
item_rate = DataGridView1.Rows(i).Cells(3).Value
i_amt = DataGridView1.Rows(i).Cells(4).Value
str1 = "insert into item values(" & bill.Text & ", " & Barcode & ", '" & Item_name & "'," & item_rate & "," & Qty & "," & i_amt & ")"
cmd = New OleDbCommand(str1, cnn)
dap = New OleDbDataAdapter(cmd)
ds = New DataSet
dap.Fill(ds)
Next
MessageBox.Show("Record Added")
bill.Clear()
paymenttype.ResetText()
cName.Clear()
Address.Clear()
phoneno.Clear()
DataGridView1.Rows.Clear()
Tamount.Text = "00"
Discount.Clear()
Namount.Clear()
End If
推荐答案
您必须将数据正确传递给SQL语句。你创建insert语句的方式可能是缺少引号(在不知道表格的情况下无法分辨),所以你会从中得到错误。
但是不要构造包含insert语句的String
,而是使用预准备语句。这有几个优点:您不必担心数据类型,转义文本中的引号或SQL注入等内容。您可以在 MSDN [ ^ ]。
You'll have to pass the data correctly to your SQL statement. The way you've created your insert statement is probably missing quotes (can't tell without knowing what the table looks like), so you'll get the error from that.
But instead of constructing aString
containing your insert statement, use a prepared statement. This has a couple of advantages: you don't have to worry about data types, escaping quotes in the text, or things such as SQL injection. You can find such an example at MSDN[^].
这篇关于如何删除此错误=“条件表达式中的数据类型不匹配”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文