简化搜索代码

时间:2015-07-27 09:51:45

标签: vb.net ms-access-2007

我正在vb.net中开发一个应用程序并作为Backend

进行访问

在搜索表单中,我有六个控件 - loanno,name,contact,sname,vno,vname。我想在任何给定的组合场景中搜索数据。我的搜索查询在下面给出

  If lno_txt.Text = "" And pname_txt.Text = "" And con_txt.Text = "" And sname_txt.Text = "" And vname_txt.Text = "" And vno_txt.Text = "" Then
        MsgBox("Please select any category")

    ElseIf lno_txt.Text <> "" And pname_txt.Text <> "" Then
        Dim sql As String = "select * from party where lno like '%" + lno_txt.Text + "%' and pname like '%" + pname_txt.Text + "%'"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

    ElseIf lno_txt.Text <> "" And con_txt.Text <> "" Then
        Dim sql As String = "select * from party where lno like '%" + lno_txt.Text + "%' and (pc1 like '%" + con_txt.Text + "%' or pc2 like '%" + con_txt.Text + "%' or pc3 like '%" + con_txt.Text + "%')"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

    ElseIf lno_txt.Text <> "" And sname_txt.Text <> "" Then
        Dim sql As String = "select * from party where lno like '%" + lno_txt.Text + "%' and (sname like '%" + sname_txt.Text + "%' or sname2 like '%" + sname_txt.Text + "%' or sname3 like '%" + sname_txt.Text + "%')"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

    ElseIf lno_txt.Text <> "" And vno_txt.Text <> "" Then
        Dim sql As String = "select * from party where lno like '%" + lno_txt.Text + "%' and vno like '%" + vno_txt.Text + "%'"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

    ElseIf lno_txt.Text <> "" And vname_txt.Text <> "" Then
        Dim sql As String = "select * from party where lno like '%" + lno_txt.Text + "%' and vname-yr like '%" + vname_txt.Text + "%'"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

    ElseIf sname_txt.Text <> "" And con_txt.Text <> "" Then
        Dim sql As String = "select * from party where (sname like '%" + sname_txt.Text + "%' or sname2 like '%" + sname_txt.Text + "%' or sname3 like '%" + sname_txt.Text + "%') and (pc1 like '%" + con_txt.Text + "%' or pc2 like '%" + con_txt.Text + "%' or pc3 like '%" + con_txt.Text + "%') "
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con1)
        Dim ds As New DataSet()
        da.Fill(ds, "aa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "aa"

任何人都可以简化上述搜索编码

先谢谢

1 个答案:

答案 0 :(得分:0)

使用所有过滤器创建唯一查询并询问条件或参数是否为null。

EJ。

select * from mytable where (filter1 = @param or @param is null)
and (filter2 = @param2 or @param2 is null)
and (filter3 = @param3 or @param3 is null)

另外,要注意使用参数而不是连接字符串,您的代码要求sql注入。另外,明确指定字段,避免使用*