1

我需要从表单上的文本框“Purchaser”中输入的文本开始显示“Purchaser”列中的数据。我正在使用 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 开头的购买者,变量“行”总是为零。

4

2 回答 2

2

那应该是:

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()
于 2012-09-23T15:26:22.790 回答
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
于 2012-09-23T15:27:24.480 回答