vb.net 一个子程序中的多个搜索语句

vb.net multiple search statements in one subroutine

我有 5 个 Select 语句搜索构建搜索变量的 SQLite 数据库
一个名为 frmBuildSearches 的表单并传递给一个带有 DataGridView
的表单 我要克服的问题是我需要为每个构建的搜索类型设置 5 个子例程
我想将 5 个搜索语句放在一个子例程中,然后在调用该子例程时做出决定
将使用哪个搜索语句。这是我第一次使用 "Using" 语句
这是挑战开始的时候,经过大量的试验和错误后停止了
下面的代码是一个子例程,其中 4 个搜索语句被注释掉
问题 如何使用“Using”语句将搜索语句嵌入到一个子例程中?

    Private Sub MoRangeYr()
    Dim intID As Integer
    Dim strDate As String
    Dim strTxType As String
    Dim strAmt As Decimal
    Dim strCKNum As String
    Dim strDesc As String
    Dim strBal As Decimal
    Dim rowCount As Integer
    Dim maxRowCount As Integer
    Dim emptyStr As String = "  "

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()
        '========================
        'Designed Searches Below
        'All
        'Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM TxData", conn)
        'MoYr
        'Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM TxData WHERE txSearchMonth = '{gvFromMonth}' AND txYear = '{gvYear}' ", conn)
        'TxMoYr
        'Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM TxData WHERE txType = '{gvTxType}' AND txSearchMonth = '{gvFromMonth}'AND txYear = '{gvYear}' ", conn)
        'Year
        'Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM TxData WHERE txYear = '{gvYear}' ", conn)
        '=========================
        'MoRangeYr
        Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM TxData WHERE  txSearchMonth = '{gvFromMonth}' AND txSearchMonth = '{gvToMonth}' AND txYear = '{gvYear}' ", conn)

            Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader

                While rdr.Read()
                    intID = CInt((rdr("TID")))
                    strDate = rdr("txSortDate").ToString
                    strTxType = rdr("txType").ToString
                    strAmt = CDec(rdr("txAmount"))
                    strCKNum = rdr("txCKNum").ToString
                    strDesc = rdr("txDesc").ToString
                    strBal = CDec(rdr("txBalance"))
                    dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                    dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                    'dgvTX.Columns(6).DefaultCellStyle.Format = "C"'Adds the $ sign and commas
                    dgvTX.Rows.Add(intID, strDate, strTxType, strAmt, strCKNum, strDesc, strBal, emptyStr)
                    Dim dateToday = Date.Today


                    Dim lblDate As String = dateToday.ToString("MMM" & " " & "d" & " " & "yyyy")
                    lblAmt.Text = "Current Balance  " & lblDate
                    tbBal.Text = "$ " & String.Format("{0:n}", strBal)
                    rowCount = rowCount + 1
                End While

                dgvTX.Sort(dgvTX.Columns(0), ListSortDirection.Descending)
                If rowCount <= 25 Then
                    maxRowCount = 25 - rowCount
                    For iA = 1 To maxRowCount
                        dgvTX.Rows.Add(" ")
                    Next
                End If
                rdr.Close()
            End Using
        End Using
        conn.Close()
    End Using
    tbBal.Focus()

End Sub

这段代码决定使用哪个子路由

    Private Sub frmViewTX_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    StyleDGV()

    If gvTEST Is "All" Then
        PopulateDGV()
    ElseIf gvTEST Is "MoYr" Then
        JustOne()
    ElseIf gvTEST Is "Year" Then
        YearOnly()
    ElseIf gvTEST Is "TxMoYr" Then
        TxMoYr()
    ElseIf gvTEST Is "MoRangeYr" Then
        MoRangeYr()
    End If
    'MoRangeYr
End Sub

正如我已经说过的,确保使用参数化查询!顺便说一下,问题的基本答案是您可以在创建 SQLiteCommand 对象和实际 运行 命令之间更改 CommandText 属性:

Private Sub MoRangeYr()
 
    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
    Using cmd As New SQLiteCommand("",conn)
        
        If gvTEST = "ALL" Then
            cmd.CommandText = "SELECT * FROM TxData"
        Else If gvTEST = "MoYr" Then
            cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth = $gvFromMonth' AND txYear = $gvYear " 
            cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
            cmd.Parameters.AddWithValue("$gvYear", gvYear)
        Else If gvTEST = "TxMoYr" Then
            cmd.CommandText = "SELECT * FROM TxData WHERE txType = $gvTxType AND txSearchMonth = $gvFromMonth AND txYear = $gvYear "
            cmd.Parameters.AddWithValue("$gvTxType", gvTxType)
            cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
            cmd.Parameters.AddWithValue("$gvYear", gvYear)
        Else If gvTEST = "MoRangeYr" Then
            cmd.CommandText = "SELECT * FROM TxData WHERE txYear = $gvYear "
            cmd.Parameters.AddWithValue("$gvYear", gvYear)
        End If   

        conn.Open()
        Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
            dgvTX.DataSource = rdr

             dgvTX.Columns(3).DefaultCellStyle.Format = "N"
             dgvTX.Columns(6).DefaultCellStyle.Format = "N"
        End Using

        lblAmt.Text = $"Current Balance  {DateTime.Today:MMMM d yyyy}"
        ' tbBal.Text = "${strBal:n}" ' This was wrong in the original code

        dgvTX.Sort(dgvTX.Columns(0), ListSortDirection.Descending)

    End Using
    End Using
End Sub

您可能还想查看 DataBinding 这个结果。您正在编写大量额外的代码来完成 GridView 知道如何为您完成的工作。还有一件事:尝试在 SQL 中添加一个 dgvTX.Sort(),而不是在最后给出该顺序。