首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

机房收费系统之结合查询:基本字符串拼接方法和存储过程实现

2013-03-16 
机房收费系统之组合查询:基本字符串拼接方法和存储过程实现组合查询是费了不少功夫,查了不少资料,主要的两

机房收费系统之组合查询:基本字符串拼接方法和存储过程实现

组合查询是费了不少功夫,查了不少资料,主要的两种思路是:字符串拼接和结合存储过程的使用.

 

第一种方法是通过最简单的方法来实现的,三层架构是为了解耦,对于组合查询就有点不太好处理,下面是我处理的过程,

程序界面:

机房收费系统之结合查询:基本字符串拼接方法和存储过程实现

U层(界面层):手机数据,传递给实体

 ''' <summary>    ''' 获取数据的值后传递给实体,并查找后数据显示给用户    ''' </summary>    ''' <param name="sender"></param>    ''' <param name="e"></param>    ''' <remarks></remarks>    Private Sub cmdQuery_Click(sender As Object, e As EventArgs) Handles cmdQuery.Click        Dim enselectString As New Entity.SelectString        Dim table As New DataTable        '代替变量        Dim f1 As String        Dim f2 As String        Dim f3 As String        '选择后转换成数据库识别的字段        Select Case cboFieldA.Text            Case "学号"                f1 = "studentId"            Case "姓名"                f1 = "studentName"            Case "性别"                f1 = "sex"            Case "系别"                f1 = "department"            Case "年级"                f1 = "grade"            Case "班级"                f1 = "stuClass"            Case "备注"                f1 = "studentStyle"            Case "卡号"                f1 = "cardId"            Case Else                f1 = ""        End Select        Select Case cboFieldB.Text            Case "学号"                f2 = "studentId"            Case "姓名"                f2 = "studentName"            Case "性别"                f2 = "sex"            Case "系别"                f2 = "department"            Case "年级"                f2 = "grade"            Case "班级"                f2 = "stuClass"            Case "备注"                f2 = "studentStyle"            Case "卡号"                f2 = "cardId"            Case Else                f2 = ""        End Select        Dim f4 As String        Select Case cboRelationA.Text            Case "或"                f4 = "or"            Case "与"                f4 = "and"            Case Else                f4 = ""        End Select        Select Case cboFieldC.Text            Case "学号"                f3 = "studentId"            Case "姓名"                f3 = "studentName"            Case "性别"                f3 = "sex"            Case "系别"                f3 = "department"            Case "年级"                f3 = "grade"            Case "班级"                f3 = "stuClass"            Case "备注"                f3 = "studentStyle"            Case "卡号"                f3 = "cardId"            Case Else                f3 = ""        End Select        '将参数传递给实体        enselectString.Work_cboFieldA = f1        enselectString.Work_cboFieldB = f2        enselectString.Work_cboFieldC = f3        enselectString.Work_cboOperatorA = cboOperatorA.Text        enselectString.Work_cboOperatorB = cboOperatorB.Text        enselectString.Work_cboOperatorC = cboOperatorC.Text        enselectString.Work_cboRelationA = f4        enselectString.Work_cboRelationB = cboRelationB.Text        enselectString.Work_txtConditionA = txtConditionA.Text        enselectString.Work_txtConditionB = txtConditionB.Text        enselectString.Work_txtConditionC = txtConditionC.Text        '将查询结果显示出来给用户        Dim InquireStudentInfoFA As New Facade.StudentManageFA        table = InquireStudentInfoFA.InquireStudentInfoBll(enselectString)        DGW.DataSource = table '数据的绑定        '显示成自己需要的数据        DGW.Columns(0).HeaderText = "学号"        DGW.Columns(1).HeaderText = "姓名"        DGW.Columns(2).HeaderText = "卡号"        DGW.Columns(3).HeaderText = "余额"        DGW.Columns(4).HeaderText = "系别"        DGW.Columns(5).HeaderText = "年级"        DGW.Columns(6).HeaderText = "班级"        DGW.Columns(7).HeaderText = "性别"        DGW.Columns(8).HeaderText = "状态"        DGW.Columns(9).HeaderText = "备注"

 

实体层中的方法

    ''' <summary>    ''' 生成字符串    ''' </summary>    ''' <param name="table">访问不同的数据库表的名称</param>    ''' <param name="ss"></param>    ''' <returns>字符串</returns>    ''' <remarks></remarks>    Public Function SelectString(ByVal table As String, ByVal ss As SelectString) As String        Dim sql As String        Try            sql = "select * from " & table & " where " & ss.cboFieldA & " " & ss.Work_cboOperatorA & " '" & ss.Work_txtConditionA & "'"            If ss.Work_cboRelationA = "" Then                Return sql            Else                sql = sql & " " & ss.Work_cboRelationA & " " & ss.Work_cboFieldB & " " & ss.Work_cboOperatorB & " '" & ss.Work_txtConditionB & "'"                If ss.Work_cboRelationB = "" Then                    Return sql                Else                    sql = sql & " " & ss.Work_cboRelationB & " " & ss.Work_cboFieldC & " " & ss.Work_cboOperatorC & " '" & ss.Work_txtConditionC & "'"                    Return sql                End If            End If        Catch ex As Exception                   '错误处理              sql = ""            Return sql        End Try


B层:逻辑的整理

   ''' <summary>    ''' 查询学生的信息    ''' </summary>    ''' <param name="selectstring">实体选择字符串</param>    ''' <returns>返回表</returns>    ''' <remarks></remarks>    Public Function InquireStudentInfo(ByVal selectstring As Entity.SelectString) As DataTable        Dim factory As New DBFactory '实例化工厂        Dim iStudent As IStudent '定义接口        iStudent = factory.CreateStudentInfo '创建接口        Dim dt As New DataTable        Try '通过视图来获取自己需要的信息            dt = iStudent.GetInfoByString("V_QueryStudentInfo", selectstring)            Return dt        Catch ex As Exception            Return dt        End Try    End Function

D:通过字符串来获取信息

 ''' <summary>    ''' 通过字符串来获取信息    ''' </summary>    ''' <param name="table">数据库表</param>    ''' <param name="selectstring">实体选择字符串</param>    ''' <returns><返回表/returns>    ''' <remarks></remarks>    Public Function GetInfoByString(table As String, selectstring As SelectString) As DataTable Implements IStudent.GetInfoByString        Dim dt As New DataTable '声明变量        Dim selectstring1 As New Entity.SelectString        Dim sql As String '定义字符串        sql = selectstring.SelectString(table, selectstring)        Try            '调用SqlHelper执行            dt = SqlHelper.DBHelper.GetDataTable(sql, CommandType.Text)            Return dt        Catch ex As Exception            Return dt        End Try    End Function

       selectstring方法的功能是根据传入的参数合成SQL语句,包括表的名称,这样做可以对不同功能进行组合查询,但是只限于该功能所查询的数据在一张表中或是在同一视图中。

       通过这样处理的组合查询,很好的遵守了三层架构的要求,实现了解耦的目的。

 

第二种方法是:不再使用上述的那样的字符串拼接,毕竟这样的方法容易处错误,下面我请教师傅学习后思考:是怎么实现不论怎样填写条件,只传送三个参数,而且没有select判断,需要设置我的Relation默认是and关系,所以只有Relation用了一个if语句来判断是不是或关系,在只有一个条件时,那么就将三个参数赋相同的值,也就是对三个相同的条件查询and关系。这样做的好处就是避免了使用判断语句来进行拼加字符串,在存储过程中直接传入参数即可。

ALTER PROCEDURE [dbo].[PROC_Query]@cboFieldA varchar(10),@cboOperatorA varchar(10),@txtConditionA varchar(10),@cboRelationA varchar(10),@cboFieldB varchar(10),@cboOperatorB varchar(10),@txtConditionB varchar(10),@cboRelationB varchar(10),@cboFieldC varchar(10),@cboOperatorC varchar(10),@txtConditionC varchar(10)--表名称或视图名称ASdeclare @TempSql varchar(500)--临时存放sql语句--CHAR(32)是空格,CHAR(39)单引号BEGINset @TempSql='select * from V_StudentInfo where '+CHAR(32)+@cboFieldA+@cboOperatorA+CHAR(39)+@txtConditionA+CHAR(39)+CHAR(32)+@cboRelationA+CHAR(32)+@cboFieldB+@cboOperatorB+CHAR(39)+@txtConditionB+CHAR(39)++CHAR(32)+@cboRelationB+CHAR(32)+@cboFieldC+@cboOperatorC+CHAR(39)+@txtConditionC+CHAR(39)execute (@TempSql)END


 

这种方法自己还是需要后期的多多实践,要求是多这样的组合查询有很强的逻辑分析,希望在合作开发的时候加深对此方法的理解.


 

热点排行