好吧坚持这一段时间并需要建议 - 我将表格带入条形码扫描到文本字段并使用该文本字段查询数据库并从产品表返回PRICE。但是当我扫描下一个项目时,我得到下面描述的错误。它就像它同时寻找两个条形码一样...但是我将文本字段设置为在将PRICE添加到项目文本框后将其设置为清除
我的代码:
Private Sub txtTest_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
If e.KeyCode = Keys.Enter Then
Dim con As New OleDbConnection
Dim databaseprovider As String
Dim dblocation As String
databaseprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dblocation = "Data Source = C:\Users\fergus\desktop\Loft Hair Studio Till App\loft.accdb"
con.ConnectionString = databaseprovider & dblocation
Dim queryLoft As String = "SELECT Price from Services where Field1 =" & TextBox3.Text & ""
Dim command As New OleDbCommand(queryLoft, con)
con.Open()
Dim myreader As OleDbDataReader = command.ExecuteReader()
myreader.Read() 'Read the next line from the DataReader
'ListBox1.Text = myreader("price").ToString
TextBoxList.Text = myreader("price").ToString
'TextBox1.Text = myreader("price").ToString
TextBoxTest.Clear()
End If
End Sub
错误:
oledbexception未得到处理
查询表达式中的语法错误(缺少运算符)' Field1 = 61542451 61524587'
就像在同一时间搜索数据库中的2个条形码一样。
看起来很奇怪。
任何帮助表示赞赏
答案 0 :(得分:0)
我注意到你的事件处理程序名为txtTest_KeyDown,但它正在处理TextBox3。我猜你在处理扫描后想要清除TextBox3。
Private Sub TextBox3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
Using cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\users\fergus\desktop\Loft Hair Studio Till App\loft.accdb")
cn.Open()
Using cmd = New OleDbCommand("SELECT Price FROM Services WHERE Field1=?", cn)
cmd.Parameters.AddWithValue("@scan", TextBox3.Text)
Using reader = cmd.ExecuteReader()
If reader.Read Then
TextBoxList.Text = reader("price").ToString
TextBox3.Clear()
End If
End Using
End Using
End Using
End If
End Sub
此外,对于一次性资源(如连接,命令和读取器)使用Using语句也是一种很好的做法。