1. 程式人生 > >個人機房重構——組合查詢及優化

個人機房重構——組合查詢及優化

Public Class TemplateMethod
    '例項化組合查詢的實體
    Protected GroupInquire As New Entity.TemplateMethodEntity

    '定義接收查詢條件的變數
    Public txtRemark1 As String
    Public txtRemark2 As String
    Public txtRemark3 As String
    Private Sub TemplateMethod_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        dtp1.Visible = False            'DateTimePicker控制元件的可見性
        dtp2.Visible = False
        dtp3.Visible = False


        cmbName2.Enabled = False        '控制元件的可用性
        cmbName3.Enabled = False


        cmbOper2.Enabled = False
        cmbOper3.Enabled = False

        cmbGroup2.Enabled = False

        txtSelect2.Enabled = False
        txtSelect3.Enabled = False

        '自動調整列寬
        Dim i As Integer
        For i = 0 To dgvLine.Columns.Count - 1
            dgvLine.Columns(i).Width = DataGridViewAutoSizeColumnMode.AllCells
        Next

    End Sub


    '查詢



    '點選查詢按鈕
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim frm As New TemplateMethod

        '判斷查詢內容是否為空
        If cmbGroup1.Text = "" Then
            If cmbName1.Text = "" Or cmbOper1.Text = "" Then
                MsgBox("欄位名或運算子不能為空!", , "提示!")
                Exit Sub
            End If
        End If

        If txtSelect1.Text = "" And dtp1.Text = "" Then
            MsgBox("查詢內容不能為空!", , "警告")
            Exit Sub
        End If


        '判斷第二行是否為空
        If cmbGroup1.Text <> "" Then
            If cmbName1.Text = "" Or cmbOper1.Text = "" Or cmbName2.Text = "" Or cmbOper2.Text = "" Then
                MsgBox("欄位名或運算子不能為空!", , "提示!")
                Exit Sub
            ElseIf txtSelect1.Text = "" And dtp1.Text = "" Then
                MsgBox("第一行查詢條件不能為空!")
                Exit Sub
            ElseIf txtSelect2.Text = "" And dtp2.Text = "" Then
                MsgBox("第二行查詢條件不能為空!")
                Exit Sub
            End If
        End If

        '判斷第三行是否為空
        If cmbGroup2.Text <> "" Then
            If cmbName1.Text = "" Or cmbOper1.Text = "" Or cmbName2.Text = "" Or cmbOper2.Text = "" Or cmbName3.Text = "" Or cmbOper3.Text = "" Then
                MsgBox("您輸入的查詢條件不能為空!", , "提示!")
                Exit Sub
            ElseIf txtSelect1.Text = "" And dtp1.Text = "" Then
                MsgBox("第一行查詢條件不能為空!", , "提示")
                Exit Sub
            ElseIf txtSelect2.Text = "" And dtp2.Text = "" Then
                MsgBox("第二行查詢條件不能為空!", , "提示")
                Exit Sub
            ElseIf txtSelect3.Text = "" And dtp3.Text = "" Then
                MsgBox("第三行查詢條件不能為空!", , "提示")
                Exit Sub
            End If
        End If


        '將引數傳給實體
        GroupInquire.DbName = GetdbName()

        GroupInquire.cmbName1 = GetEnglish(cmbName1.Text)
        GroupInquire.cmbName2 = GetEnglish(cmbName2.Text)
        GroupInquire.cmbName3 = GetEnglish(cmbName3.Text)

        GroupInquire.cmbOper1 = cmbOper1.Text.Trim
        GroupInquire.cmbOper2 = cmbOper2.Text.Trim
        GroupInquire.cmbOper3 = cmbOper3.Text.Trim

        GroupInquire.txtSelect1 = txtRemark1                  '設定一個變數,將textBox或者DateTimePicker控制元件的值賦值後傳遞給實體
        GroupInquire.txtSelect2 = txtRemark2
        GroupInquire.txtSelect3 = txtRemark3


        GroupInquire.cmbGroup1 = GetEnglish(cmbGroup1.Text)
        GroupInquire.cmbGroup2 = GetEnglish(cmbGroup2.Text)

        GroupInquire.SqlText = Query(frm, GroupInquire)        '將拼接的字串傳遞給實體層


        '進行資料查詢並返回dt
        Dim dt As New DataTable
        Dim Generalfacade As New Facade.TemplateMethodFacade

        dt = Generalfacade.SelectInfo(GroupInquire)

        If (dt.Rows.Count = 0) Then                                 '若是沒有資料,則清空原來查詢到的資料
            MsgBox("沒有符合條件的記錄!", , "提示!")
            dt.Clear()
            dgvLine.DataSource = Nothing
            dgvLine.Refresh()
            Exit Sub
        Else
            Call Todgv(dt)              ' 若是查詢到資料,返回dt表,等待子類接收
        End If

    End Sub

    '連線字串,以便供給D層進行查詢
    Public Function Query(frm As TemplateMethod, ByVal GroupInquire As Entity.TemplateMethodEntity) As String
        Dim sql As String

        sql = GroupInquire.cmbName1 & GroupInquire.cmbOper1 & "'" & GroupInquire.txtSelect1 & "'"
        If Trim(GroupInquire.cmbGroup1) <> "" Then
            If Trim(GroupInquire.cmbGroup2) = "" Then   '有兩組查詢條件
                sql = sql & GroupInquire.cmbGroup1 & GroupInquire.cmbName2 & GroupInquire.cmbOper2 & "'" & GroupInquire.txtSelect2 & "'"
            Else
                sql = sql & GroupInquire.cmbGroup1 & GroupInquire.cmbName2 & GroupInquire.cmbOper2 & "'" & GroupInquire.txtSelect2 & "'" & GroupInquire.cmbGroup2 & GroupInquire.cmbName3 & GroupInquire.cmbOper3 & "'" & GroupInquire.txtSelect3 & "'"

            End If
        End If
        Return sql

    End Function

    ' 轉換中文字元為英文字元
    Public Overridable Function GetEnglish(cmbName As String) As String
        Return ""
    End Function

    '獲得所要查詢資料庫表
    Protected Overridable Function GetdbName() As String
        Return ""
    End Function

    ' 傳遞父類的datatable表給子類
    Protected Overridable Sub Todgv(ByVal dt As DataTable)

    End Sub

    Private Sub cmbGroup1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbGroup1.SelectedIndexChanged
        If cmbGroup1.Text = "" Then
            cmbName2.Enabled = False              '如果沒有選擇第一個組合查詢,則之後的控制元件都不可用
            cmbName3.Enabled = False
            cmbOper2.Enabled = False
            cmbOper3.Enabled = False
            cmbGroup2.Enabled = False
            txtSelect2.Enabled = False
            txtSelect3.Enabled = False
        Else
            cmbName2.Enabled = True
            cmbOper2.Enabled = True
            txtSelect2.Enabled = True
            cmbGroup2.Enabled = True


        End If
    End Sub

    Private Sub cmbGroup2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbGroup2.SelectedIndexChanged
        If cmbGroup2.Text = "" Then                    '如果沒有選擇第二個組合查詢,則之後的控制元件都不可用
            cmbName3.Enabled = False
            cmbOper3.Enabled = False
            txtSelect3.Enabled = False
        Else
            cmbName3.Enabled = True
            cmbOper3.Enabled = True
            txtSelect3.Enabled = True
        End If
    End Sub</span>