mysql - 创建多条件搜索

标签 mysql vb.net search

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

enter image description here

我的数据库表是:

PROJE ADI 表:

enter image description here

FIRMA ADI(详细信息)表:

enter image description here

供应商表:

enter image description here

SISTEM 表:

enter image description here

PROJE DURUMU 表:

它将从 FIRMA ADI(details) 表的 PROJEDURUMU 字段获取数据。

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

enter image description here

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

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

最佳答案

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

首先准备您的选择

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 的此答案

关于mysql - 创建多条件搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32993513/

相关文章:

mysql - SQL : Get rows with multiple conditions

php - 两个错误 - 在自定义管理面板上将一个项目标记为带有按钮

MySQL 子查询返回错误数据

Mysql表设计两个主键与一个主键

wpf - mvvm light icleanup 和 messenger 在 VB.NET 中注销

javascript - AngularJS:使用从输入中获取的两个参数过滤结果

javascript - 时间选择器仅在 asp.net 中

mysql - TIMESTAMPDIFF 格式转换(小时和分钟)

java - ArrayList 图书搜索

elasticsearch - 使用Apache Lucene进行 bool 查询的结果编号