VB.NET & (三層+泛型)實現組合查詢

來源:互聯網
上載者:User

標籤:組合查詢   泛型   拼接字串   vb.net組合查詢   

     對於組合查詢,真的是“費勁苦難”,當然也只是玩笑話,下邊談談自己拼接字串的辦法吧!

     首先我們要明確聲明一個實體層,定義組合查詢為實體類,將"欄位,操作符,組合關係分別設為實體類combinQuery的屬性

 '欄位1    Private _ComboFileName1 As String    Public Property ComboFileName1() As String        Get            Return _ComboFileName1        End Get        Set(value As String)            _ComboFileName1 = value        End Set    End Property    '欄位2    Private _ComboFileName2 As String    Public Property ComboFileName2() As String        Get            Return _ComboFileName2        End Get        Set(value As String)            _ComboFileName2 = value        End Set    End Property…………
     對於U層,我們除了要實現參數賦值,還要將其記錄顯示在DataGridView中,在顯示的這一階段自己採用了泛型集合,將datatable類型轉換成了集合而顯示出來

看U層:

Private Sub btnQuery_Click(sender As Object, e As EventArgs) Handles btnQuery.Click        'combinQuery_m  ,定義一個實體        Dim combinQuery_m As IList(Of Entity.RegisterEntity)   '定義泛型集合        Dim combinQuery_bll As New BLL.StusInfoMainBLL    '定義一個B層介面        Dim combinQuery As New Entity.CombinQueryEntity    '定義一個實體,作為參數        Dim table As String = "T_student"    '用到的表,與學生表聯絡起來        Dim arrayControl(2) As Control '給參數賦值          With combinQuery            '將查詢條件欄位1的值賦給實體combinQuery_m的欄位1ComboFileName1屬性              .ComboFileName1 = ComboFileName1.Text.Trim()            .ComboFileName2 = ComboFileName2.Text.Trim()            .ComboFileName3 = ComboFileName3.Text.Trim()            ''將操作符1的值賦給實體combinQuery_m的操作符1ComboSign1屬性              .ComboSign1 = ComboSign1.Text.Trim()            .ComboSign2 = ComboSign2.Text.Trim()            .ComboSign3 = ComboSign3.Text.Trim()            '將查詢內容1的值賦給實體combinQuery_m的查詢內容1txtInqure1屬性              .txtInqure1 = txtInqure1.Text.Trim()            .txtInqure2 = txtInqure2.Text.Trim()            .txtInqure3 = txtInqure3.Text.Trim()            '將組合關係1的值賦給實體combinQuery_m的組合關係1ComboRelation1屬性             .ComboRelation1 = ComboRelation1.Text.Trim()            .ComboRelation2 = ComboRelation2.Text.Trim()        End WithcombinQuery_m = combinQuery_bll.StusInfo(table, combinQuery)        DataGridView1.DataSource = combinQuery_m   '查詢到的集合
     以上代碼主要還是實現將查詢到的集合反饋到DataGridView的過程
我們還要將字串轉化,給欄位,操作符,組合關係等分別賦值

將查詢欄位(1,2,3)對應於”表“的欄位(eg: cardNo),操作符對應於關係運算子,組合關係對應於邏輯運算子(or/and)

看部分代碼:

'字串轉換        '給欄位1賦值        Select Case (combinQuery.ComboFileName1)            Case "卡號"                combinQuery.ComboFileName1 = "cardNo"            Case "學號"                combinQuery.ComboFileName1 = "studentNo"            Case "姓名"                combinQuery.ComboFileName1 = "studentName"            Case "性別"                combinQuery.ComboFileName1 = "sex"            Case "年級"                combinQuery.ComboFileName1 = "Grade"            Case "班級"                combinQuery.ComboFileName1 = "sclass"        End Select '操作符1        Select Case combinQuery.ComboSign1            Case "="                combinQuery.ComboSign1 = "="            Case ">"                combinQuery.ComboSign1 = ">"            Case "<"                combinQuery.ComboSign3 = "<"            Case "<>"                combinQuery.ComboSign1 = "<>"        End Select  '組合關係1        Select Case combinQuery.ComboRelation1            Case "或"                combinQuery.ComboRelation1 = "or"            Case "與"                combinQuery.ComboRelation1 = "and"        End Select
     由於在整個系統過程中,我們涉及到了四個組合查詢,所以定義了一個字串類型的Table,實現了代碼的複用性。以下則是拼接字串的方法:

 ''' <summary>    ''' 產生組合查詢sql語句-拼接sql字串    ''' </summary>    ''' <param name="table"></param>    ''' <param name="combinQuery">combinQuery實體</param>    ''' <returns></returns>    ''' <remarks></remarks>    Public Function CombinsqlQuery(ByVal table As String, ByVal combinQuery As Entity.CombinQueryEntity) As String        '首先,第一個查詢條件有效          Dim sql As String = "select * from " & table & " where  " & combinQuery.ComboFileName1 & " " & combinQuery.ComboSign1 & " " & combinQuery.txtInqure1.Trim()        If combinQuery.ComboRelation1 = "" Then    '如果第一個組合關係為空白,則第一個查詢條件有效              Return sql        Else     '如果第一個組合關係不為空白,則前兩個查詢條件有效              sql = sql & " " & combinQuery.ComboRelation1 & " " & combinQuery.ComboFileName2 & " " & combinQuery.ComboSign2 & " " & combinQuery.txtInqure2.Trim()            If combinQuery.ComboRelation2 = "" Then     '如果第一個組合關係不為空白,第二個組合關係為空白,則僅僅前兩個查詢條件有效                  Return sql            Else       '如果第一二組合關係不為空白,則三個查詢條件均有效                  sql = sql & " " & combinQuery.ComboRelation2 & " " & combinQuery.ComboFileName3 & " " & combinQuery.ComboSign3 & " " & combinQuery.txtInqure3.Trim()                Return sql            End If        End If        If combinQuery.ComboRelation1 <> "" And combinQuery.ComboRelation2 = "" Then     '如果第一個組合關係不為空白,第二個組合關係為空白,則前兩個查詢條件有效              sql = sql & " " & combinQuery.ComboRelation1 & " " & combinQuery.ComboFileName2 & " " & combinQuery.ComboSign2 & " " & combinQuery.txtInqure2.Trim()        ElseIf combinQuery.ComboRelation1 <> "" And combinQuery.ComboRelation2 <> "" Then '如果第一個組合關係和第二個組合關係均不為空白,則三個查詢條件有效              sql = sql        End If    End Function
由於是D層實現調用,所以我把這個過程以一個Module形式定義在了D層中,下邊看最關鍵的D層部分:

Private clsSqlhelper As DAL.sqlhelper = New DAL.sqlhelper()   '聲明並執行個體化    ''' <summary>    ''' 泛型集合,組合查詢    ''' </summary>    ''' <param name="table"></param>    ''' <param name="combinQuery"></param>    ''' <returns></returns>    ''' <remarks></remarks>    Public Function StuInfoQuery(ByVal table As String, ByVal combinQuery As Entity.CombinQueryEntity) As <span style="color:#3333ff;">IList(Of Entity.RegisterEntity</span>) Implements IDAL.IStusInfoMainDAL.StuInfoQuery        Dim dt As New DataTable        Dim myList As IList(Of Entity.RegisterEntity)     '儲存轉化後的泛型集        '調用查詢語句        'sql語句。調用Model層combinQuery類的方法CombinQuerySql()方法,返回sql語句          Dim strsql As String = CombinQueryModule.CombinsqlQuery(table, combinQuery)        dt = clsSqlhelper.Query(strsql, CommandType.Text)    '執行查詢        '將dt轉換為泛型集合       myList = EntityHelper.converToList(Of RegisterEntity)(dt)        Return myList    End Function
整個過程自己嘗試定義了工廠類和介面,所以B層直接聲明了一個變數,調用由Factory的方法返回來的介面

  '聲明並執行個體化Factory為DataAccess類    Private ReadOnly factory As DataAccess = New DataAccess    Public Function StusInfo(ByVal data As String, ByVal StusQuery As Entity.CombinQueryEntity) As IList(Of Entity.RegisterEntity)        '聲明並執行個體化變數InterfaceUser為:調用Factory.CreateUserDAL()所返回來的IUser        Dim comboQuery As IStusInfoMainDAL = factory.StusInfoMain()        Dim StusMain As IList(Of Entity.RegisterEntity)   '定義一個泛型集合        StusMain = comboQuery.StuInfoQuery(data, StusQuery)</span>        Return StusMain    End Function
     這樣組合查詢的整個過程就完成了, 整個過程的實現,很好的遵守了三層架構的要求,實現瞭解耦的目的。當然除了拼接字串還有使用預存程序實現的方法,這樣就無需大量賦值,而且也就不用拼接字串了,直接將這些全部定義在了預存程序中,然後再預存程序中直接傳入參數就好了,雖然自己明白整個過程的實現,但是還是欠缺實踐,希望在合作開發的過程中能夠熟練對於各種技巧的使用。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.