创建多个标准搜索

时间:2015-10-07 13:28:33

标签: 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(详细信息)表的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

1 个答案:

答案 0 :(得分:1)

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

首先准备好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

的答案