我需要显示“购买者”列中的数据,从表单上文本框“购买者”中输入的文本开始。我正在使用MS Access 2003数据库。 为此,我使用以下内容......
Dim query = "SELECT * FROM Details WHERE [Purchaser] LIKE '" & Purchaser.Text & "*'"
Dim dc = New OleDbCommand(query, cn)
Dim rows = dc.ExecuteNonQuery
cn.Close()
If rows = 0 Then
'Show a form for new entry
Else
Dim oleadap = New OleDbDataAdapter(query, cn)
Dim dset As DataSet = Nothing
oleadap.Fill(dset, "Details")
For i = 0 To rows
Dim purName = dset.Tables("Details").Rows(i).Item("Purchaser").ToString
Dim purAddr = dset.Tables("Details").Rows(i).Item("Address").ToString
'Populate a list
Next
End If
即使我在数据库中检查以A开头的买方,变量'rows'也总是为零。
答案 0 :(得分:1)
应该是:
Dim query = "SELECT * FROM Details WHERE [Purchaser] LIKE '" _
& Purchaser.Text & "%'"
在MS Access中,通配符为星号(*),在它之外,通配符为百分号(%)
此外,您有ExecuteNonQuery,但事实并非如此。您正在执行查询,以下是一些测试说明。
Dim query = "SELECT * FROM Details WHERE [Purchaser] LIKE '" _
& Purchaser.Text & "%'"
Dim dc = New OleDbCommand(query, cn)
Dim rows As OleDb.OleDbDataReader
rows = dc.ExecuteReader
If rows.HasRows Then
Do While rows.Read()
Console.WriteLine(rows("Purchaser"))
Loop
End If
Console.ReadLine()
答案 1 :(得分:0)
您可以使用%
代替*
吗?另一个,使用参数。
Dim query = "SELECT * FROM Details WHERE [Purchaser] LIKE @purc & '%' "
Dim dc = New OleDbCommand(query, cn)
dc.Parameters.AddWithValue("@purc", Purchaser.Text)
Dim rows = dc.ExecuteNonQuery