VB.Net参数化LIKE查询

时间:2014-09-29 12:43:30

标签: vb.net ms-access sql-like

我正在编写一个代码,用于使用zone-rate-customer_number格式过滤帐号。一个例子是125-12-35,其中125是区域,12是费率,35是客户编号。在我的搜索文本框中,我想输入区域和客户编号,这将排除费率。这是我的代码

        Dim search As String = txtsearch.Text
        Dim sql As String = "SELECT * FROM tblCustomer WHERE [account no] LIKE '%' + @search + '%'"

        Dim cmd As OleDbCommand = New OleDbCommand()
        cmd.Connection = Con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sql
        cmd.Parameters.AddWithValue("[account no]", search)

如何只使用区域和客户编号在我的表格中搜索?

1 个答案:

答案 0 :(得分:2)

首先,您应该单独收集“zone”和“customer_number”(例如,在单独的文本框中)。然后你可以使用

进行LIKE搜索
' test data
Dim zone As String = "125"
Dim customer_number As String = "35"

Using cmd As New OleDbCommand
    cmd.Connection = con
    cmd.CommandText =
            "SELECT * FROM tblCustomer WHERE [account no] LIKE ?"
    cmd.Parameters.AddWithValue("?", zone & "-%-" & customer_number)
    Using rdr As OleDbDataReader = cmd.ExecuteReader
        Do While rdr.Read
            Console.WriteLine(rdr("account no"))
        Loop
    End Using
End Using