我正在尝试编写用于多条件搜索的代码。我的表格是这样的:

mysql - 创建多个条件搜索-LMLPHP

我的数据库表也是:

PROJE ADI表:

mysql - 创建多个条件搜索-LMLPHP

FIRMA ADI(详细信息)表:

mysql - 创建多个条件搜索-LMLPHP

供应商表:

mysql - 创建多个条件搜索-LMLPHP

SISTEM表:

mysql - 创建多个条件搜索-LMLPHP

PROJE DURUMU表:

它将从FIRMA ADI(详细信息)表的PROJEDURUMU字段中获取数据。

所有结果将显示在另一个窗口中,如下所示:

mysql - 创建多个条件搜索-LMLPHP

我已经开始编写代码,但无法处理如何组合复选框以及如何将结果发送到我的结果窗口。

Imports MySql.Data.MySqlClient
Public Class ProjeAra

    Private Sub ProjeAra_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If con.State = ConnectionState.Open Then
           con.Close()
        End If

        Dim readerb As MySqlDataReader
        Try
            con.Open()
            Dim sorgub As String
           sorgub = "select ID,PROJEADI from projects"
            Dim cmdb As New MySqlCommand(sorgub, con)
            readerb = cmdb.ExecuteReader
            ComboBox1.Items.Clear()
            While readerb.Read
                Dim systb = readerb.GetString("PROJEADI")
                ComboBox1.Items.Add(systb)
            End While
            readerb.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try

        Dim readerc As MySqlDataReader
        Try
            con.Open()
            Dim sorguc As String
            sorguc = "select DISTINCT TEKLIFFIRMA from details"
            Dim cmdc As New MySqlCommand(sorguc, con)
            readerc = cmdc.ExecuteReader
            ComboBox2.Items.Clear()
           While readerc.Read
                Dim systc = readerc.GetString("TEKLIFFIRMA")
                ComboBox2.Items.Add(systc)
            End While
            readerc.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try


        Dim readera As MySqlDataReader
        Try
            con.Open()
            Dim sorgua As String
            sorgua = "select DISTINCT VENDOR from vendor"
            Dim cmda As New MySqlCommand(sorgua, con)
            readera = cmda.ExecuteReader
            ComboBox3.Items.Clear()
            While readera.Read
                Dim systa = readera.GetString("VENDOR")
                ComboBox3.Items.Add(systa)
            End While
            readera.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try

        Dim readerd As MySqlDataReader
        Try
        con.Open()
            Dim sorgud As String
            sorgud = "select STATUS from durum"
            Dim cmdd As New MySqlCommand(sorgud, con)
            readerd = cmdd.ExecuteReader
             ComboBox4.Items.Clear()
            While readerd.Read
                Dim systd = readerd.GetString("STATUS")
                ComboBox4.Items.Add(systd)
            End While
            readerd.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' Build a list of values based on combo boxes with a selected index.
        Dim values As New List(Of String)

        ' Build an array of combo boxes we want to process.
        For Each cb As ComboBox In New ComboBox() {ComboBox1, ComboBox2, ComboBox3, ComboBox4}
            ' Check if the current combo box has an index selected.
            If cb.SelectedIndex <> -1 Then
                values.Add(cb.Text)
            End If
        Next

        ' Do something with the values.
        MessageBox.Show(String.Join(", ", values.ToArray))

        ' For example, build a where clause.
        ' If you do this, be sure to sanitize the values.
        '   MessageBox.Show("WHERE 0=1 " & String.Join(" OR Field=", values.ToArray))

    End Sub
End Class

最佳答案

您的问题尚不完全清楚,但在我看来,您需要基于复选框来建立标准。

首先准备您的select

Dim sql As String = "Select .... From ... Where 1=1"


然后,为每个复选框执行此操作

If chk.Checked Then
    sql += " AND Field1 = '" & cb.Text & "'"
End If
. . . . .  .


对于每个组合框,请执行此操作

' NOTE: good practice - to have an empty item at first position,
' so user can select it as to say, "I select nothing". In this case you
' do If cbo.Selectedndex > 0 Then
If cbo.Selectedndex > -1 Then
    sql += " AND Field1 = '" & cbo.Text & "'"
End If
. . . . .  .


这是一般的想法。另外,请记住参数化,即

sql += " AND Field1 = @1"


查看此答案以获取parameterization

09-06 17:02