2017-05-15 5 views
-1

私は4つのコンボボックスとボタンを持つフォームを持っています。Crystal Report + VB net + Mysql ifおよびElsIfの数

  1. ライセンス
  2. メーカー
  3. タイプ
  4. スケール

がどのように私は、たとえば、他の2なしの値の2を選択することができます。このボタンは、コンボボックスの4つの値を検索します、メーカーとスケール?選択した値を検索して表示できるようにする必要があります。今、私はこのコードしか持っていませんが、私は4つの値が必要です。 if文combox1 =クエリ1、もしコンボ1 +コンボ3 =クエリ3、など......

If ComboBox1.Text = "All" Then 
     Me.Cursor = Cursors.WaitCursor 
     Try 
      Dim ds As New DataSet 
      Dim query As String 
      query = "select * from product order by id_maker asc, id_types asc, id_scale asc,name asc" 
      Dim dscmd As New MySqlDataAdapter(query, con) 
      dscmd.Fill(ds, "PAYPRODUCTTOTAL") 
      con.Close() 
      Dim cryds As New All_Database_Report 
      cryds.SetDataSource(ds.Tables(0)) 
      CrystalReportViewer1.ReportSource = cryds 
      CrystalReportViewer1.Refresh() 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 
    ElseIf ComboBox1.Text <> "All" Then 
     If ComboBox2.Text = Nothing Then 
       MsgBox("Select a Maker", MsgBoxStyle.Exclamation) 
      ElseIf ComboBox3.Text = Nothing Then 
       MsgBox("Select a Type", MsgBoxStyle.Exclamation) 
      ElseIf ComboBox4.Text = Nothing Then 
       MsgBox("Select a Scale", MsgBoxStyle.Exclamation) 
      Else 
       Try 
        Dim ds As New DataSet 
        Dim query As String 
        p(0) = New MySqlParameter("@License", MySqlDbType.String) 
        p(0).Value = ComboBox1.Text 
        p(1) = New MySqlParameter("@Maker", MySqlDbType.String) 
        p(1).Value = ComboBox2.Text 
        p(2) = New MySqlParameter("@Type", MySqlDbType.String) 
        p(2).Value = ComboBox3.Text 
        p(3) = New MySqlParameter("@Scale", MySqlDbType.String) 
        p(3).Value = ComboBox4.Text 
        query = "select * from product WHERE license = @License AND id_maker = @Maker AND id_types = @Type AND id_scale = @Scale order by id_maker asc, id_types asc, id_scale asc,name asc" 
        Dim dscmd As New MySqlDataAdapter(query, con) 
        dscmd.SelectCommand.Parameters.Add(p(0)) 
        dscmd.SelectCommand.Parameters.Add(p(1)) 
        dscmd.SelectCommand.Parameters.Add(p(2)) 
        dscmd.SelectCommand.Parameters.Add(p(3)) 
        dscmd.Fill(ds, "PAYPRODUCTTOTAL") 
        con.Close() 
        Dim cryds As New All_Database_Report 
        cryds.SetDataSource(ds.Tables(0)) 
        CrystalReportViewer1.ReportSource = cryds 
        CrystalReportViewer1.Refresh() 
       Catch ex As Exception 
        MessageBox.Show(ex.Message) 
       End Try 
      End If 
     End If 

     Me.Cursor = Cursors.Default 

答えて

1

は値があるかどうかを確認するために追加した場合、私は、外であることを変更するには何をしますか空の場合は空にして、コマンドにパラメータを追加しないでください。

SQLの場合、Where 1 = 1を追加して、If文で各フィールド/値をラップして空白をチェックします。 1 = 1で追加すると、すべてのフィールド/値のフォーマットが同じになります。フィールド値=

If ComboBox1.Text = "All" Then 
      Me.Cursor = Cursors.WaitCursor 
      Try 
       Dim ds As New DataSet 
       Dim query As String 
       query = "select * from product order by id_maker asc, id_types asc, id_scale asc,name asc" 
       Dim dscmd As New MySqlDataAdapter(query, con) 
       dscmd.Fill(ds, "PAYPRODUCTTOTAL") 
       con.Close() 
       Dim cryds As New All_Database_Report 
       cryds.SetDataSource(ds.Tables(0)) 
       CrystalReportViewer1.ReportSource = cryds 
       CrystalReportViewer1.Refresh() 
      Catch ex As Exception 
       MessageBox.Show(ex.Message) 
      End Try 
     ElseIf ComboBox1.Text <> "All" Then 
      If ComboBox2.Text = Nothing Then 
       MsgBox("Select a Maker", MsgBoxStyle.Exclamation) 
      ElseIf ComboBox3.Text = Nothing Then 
       MsgBox("Select a Type", MsgBoxStyle.Exclamation) 
      ElseIf ComboBox4.Text = Nothing Then 
       MsgBox("Select a Scale", MsgBoxStyle.Exclamation) 
      Else 
       Try 
        Dim ds As New DataSet 
        Dim query As StringBuilder = New StringBuilder() 

        p(0) = New MySqlParameter("@License", MySqlDbType.String) 
        p(0).Value = ComboBox1.Text 
        p(1) = New MySqlParameter("@Maker", MySqlDbType.String) 
        p(1).Value = ComboBox2.Text 
        p(2) = New MySqlParameter("@Type", MySqlDbType.String) 
        p(2).Value = ComboBox3.Text 
        p(3) = New MySqlParameter("@Scale", MySqlDbType.String) 
        p(3).Value = ComboBox4.Text 

        With query 
         .Append("select * from product WHERE 1=1 ") 
         If p(0).Value <> "" Then .Append(" AND license = @License ") 
         If p(1).Value <> "" Then .Append(" AND id_maker = @Maker ") 
         If p(2).Value <> "" Then .Append(" AND id_types = @Type ") 
         If p(3).Value <> "" Then .Append(" AND id_scale = @Scale ") 
         .Append(" order by id_maker asc, id_types asc, id_scale asc,name asc ") 
        End With 

        Dim dscmd As New MySqlDataAdapter(query.ToString(), con) 
        If p(0).Value <> "" Then dscmd.SelectCommand.Parameters.Add(p(0)) 
        If p(1).Value <> "" Then dscmd.SelectCommand.Parameters.Add(p(1)) 
        If p(2).Value <> "" Then dscmd.SelectCommand.Parameters.Add(p(2)) 
        If p(3).Value <> "" Then dscmd.SelectCommand.Parameters.Add(p(3)) 
        dscmd.Fill(ds, "PAYPRODUCTTOTAL") 
        con.Close() 
        Dim cryds As New All_Database_Report 
        cryds.SetDataSource(ds.Tables(0)) 
        CrystalReportViewer1.ReportSource = cryds 
        CrystalReportViewer1.Refresh() 
       Catch ex As Exception 
        MessageBox.Show(ex.Message) 
       End Try 
      End If 
     End If 

     Me.Cursor = Cursors.Default 
+0

Ok。私は後で試してみてください...共有のおかげで – Jamyz

+0

ありがとうが、私は "StringBuilder"は定義されていない1つの問題があります。 ??なにか提案を ? – Jamyz

+1

Imports System.Textは役に立ちます。 – EJD