在VB.NET 机房收费系统的组合查询中我写了完了一条线,然后看了其他同学对于组合查询的代码我感觉自己的代码很幼稚,不过还是想发表一下这个代码花了我半天的时间完成的。接下来就贴上图和代码。
组合查询的窗体:
总结U层代码:
Public ClassUIFrmOperatorWorkRecord
Private SubUIFrmOperatorWorkRecord_Load(ByVal sender As System.Object,ByVal e AsSystem.EventArgs) Handles MyBase.Load
’定义字符串数组
DimstrFIEldname(6) As String
Dim strOperator(3) As String
Dim strCompositionRelation(1) As String
DimIntTemp As Integer
'给字符串数组赋值
For IntTemp = 0 To 6
Select Case IntTemp
Case 0
strFIEldname(0) ="教师"
strOperator(0) ="="
strCompositionRelation(0) ="或"
Case 1
strFIEldname(1) ="级别"
strOperator(1) =">"
strCompositionRelation(1) ="与"
Case 2
strFIEldname(2) ="登录日期"
strOperator(2) ="<"
Case 3
strFIEldname(3) ="登录时间"
strOperator(3) ="="
Case 4
strFIEldname(4) ="注销日期"
Case 5
strFIEldname(5) ="注销时间"
Case 6
strFIEldname(6) ="机器名"
End Select
Next IntTemp
'控件增加字段
cboFIEldname1.Items.AddRange(strFIEldname)
cboFIEldname2.Items.AddRange(strFIEldname)
cboFIEldname3.Items.AddRange(strFIEldname)
cboOperator1.Items.AddRange(strOperator)
cboOperator2.Items.AddRange(strOperator)
cboOperator3.Items.AddRange(strOperator)
cboCompositionRelation1.Items.AddRange(strCompositionRelation)
cboCompositionRelation2.Items.AddRange(strCompositionRelation)
End Sub
Private Sub BtnCheck_Click(ByVal sender AsSystem.Object,ByVal e As System.EventArgs) Handles BtnCheck.Click
Dim ECombination As NewEntity.Combination
Dim BCombination As NewBLL.BLLOperatorWorkRecord
Dim dt As New Datatable
'把信息传给ECombination
ECombination.FIEldname1 =Trim(cboFIEldname1.Text)
ECombination.FIEldname2 =Trim(cboFIEldname2.Text)
ECombination.FIEldname3 =Trim(cboFIEldname3.Text)
ECombination.Operator1 =Trim(cboOperator1.Text)
ECombination.Operator2 =Trim(cboOperator2.Text)
ECombination.Operator3 =Trim(cboOperator3.Text)
ECombination.CompositionRelation1 =Trim(cboCompositionRelation1.Text)
ECombination.CompositionRelation2 =Trim(cboCompositionRelation2.Text)
ECombination.ChcekContent1 =Trim(txtChcekContent1.Text)
ECombination.ChcekContent2 =Trim(txtChcekContent2.Text)
ECombination.ChcekContent3 =Trim(txtChcekContent3.Text)
'组合查询
dt =BCombination.CombinationCheck(ECombination)
'把查到的结果传给gvwVIEwOperatorWorkRecord控件显示出来
gvwVIEwOperatorWorkRecord.DataSource =dt
End Sub
Private Sub BtnExit_Click(ByVal sender AsSystem.Object,ByVal e As System.EventArgs) Handles BtnExit.Click
Me.HIDe()
End Sub
End Class
B层代码
Public ClassBLLOperatorWorkRecord
''' <summary>
''' 组合查询
''' </summary>
''' <paramname="Combination"></param>
''' <returns></returns>
''' <remarks></remarks>
Function CombinationCheck(ByVal CombinationAs Entity.Combination) As Datatable
Dim ECombination As NewEntity.Combination '定义一个实体类
Dim DCombiantion As NewDAL.DALWorkLogInfo '定义一个DALWorkLogInfo来调用D层的方法
Dim strsqlString As String '定义一个存SQL语句
Dim dt As New Datatable'定义一个Datatable来存储查询出来的语句
ECombination = Combination '把传过来的值传给ECombination
strsqlString =CombinationsqlString(ECombination) '调用 CombinationsqlString函数来拼接SQL语句
dt =DCombiantion.CheckWorkLogInfo(strsqlString)'调用D层的CheckWorkLogInfo方法来查询,并把查询到的结果传给dt
Return dt
End Function
''' <summary>
''' 改变把中文字符编程英文
''' </summary>
''' <paramname="FIEld"></param>
''' <returns></returns>
''' <remarks></remarks>
Function ChangeFIEld(ByVal FIEld As String)As String
Select Case FIEld
Case "教师"
FIEld = " OperatorID "
Case "级别"
FIEld = " OperatorLevel"
Case "登录日期"
FIEld = " logonDate"
Case "登录时间"
FIEld = " logonTime"
Case "注销日期"
FIEld = " logoutDate"
Case "注销时间"
FIEld = " logoutTime"
Case "机器名"
FIEld = " OperatorUseComputer "
Case "或"
FIEld = " or"
Case "与"
FIEld = " and"
End Select
Return FIEld
End Function
''' <summary>
''' 组合条件
''' </summary>
''' <paramname="Combination"></param>
''' <returns></returns>
''' <remarks></remarks>
Function CombinationsqlString(ByValCombination As Entity.Combination) As String
Dim ECombination As NewEntity.Combination '定义一个实体类
'定义字符串存储查询的内容
Dim strsqlString1 As String
Dim strsqlString2 As String
Dim strsqlString3 As String
Dim strsqlAll As String
ECombination = Combination
'拼接sql字符串
ECombination.FIEldname1 =ChangeFIEld(Combination.FIEldname1)
ECombination.FIEldname2 =ChangeFIEld(Combination.FIEldname2)
ECombination.FIEldname3 =ChangeFIEld(Combination.FIEldname3)
ECombination.ChcekContent1 ="'" + Combination.ChcekContent1 + "'"
ECombination.ChcekContent2 ="'" + Combination.ChcekContent2 + "'"
ECombination.ChcekContent3 ="'" + Combination.ChcekContent3 + "'"
ECombination.CompositionRelation1 =ChangeFIEld(Combination.CompositionRelation1)
ECombination.CompositionRelation2 =ChangeFIEld(Combination.CompositionRelation2)
strsqlString1 = ECombination.FIEldname1+ ECombination.Operator1 + ECombination.ChcekContent1 +ECombination.CompositionRelation1
strsqlString2 = ECombination.FIEldname2+ ECombination.Operator2 + ECombination.ChcekContent2 +ECombination.CompositionRelation2
strsqlString3 = ECombination.FIEldname3+ ECombination.Operator3 + ECombination.ChcekContent3
'检查某些字符串为空的情况
Select Case True
Case ECombination.FIEldname2 ="" And ECombination.CompositionRelation1 <> ""
strsqlString1 =ECombination.FIEldname1 + ECombination.Operator1 + ECombination.ChcekContent1
strsqlString2 = ""
strsqlString3 = ""
Case ECombination.FIEldname3 ="" And ECombination.CompositionRelation2 <> ""
strsqlString2 =ECombination.FIEldname2 + ECombination.Operator2 + ECombination.ChcekContent2
strsqlString3 = ""
strsqlAll =AddString(strsqlString1,strsqlString2,strsqlString3)
CaseECombination.CompositionRelation1 = ""
strsqlString1 =ECombination.FIEldname1 + ECombination.Operator1 + ECombination.ChcekContent1
strsqlString2 = ""
strsqlString3 = ""
CaseECombination.CompositionRelation2 = ""
strsqlString2 =ECombination.FIEldname2 + ECombination.Operator2 + ECombination.ChcekContent2
strsqlString3 = ""
End Select
'把查询字符串拼接在一起
strsqlAll = AddString(strsqlString1,strsqlString3)
Return strsqlAll
End Function
''' <summary>
''' 字符串拼接
''' </summary>
''' <paramname="str1"></param>
''' <paramname="str2"></param>
''' <paramname="str3"></param>
''' <returns></returns>
''' <remarks></remarks>
Function AddString(ByVal str1 As String,ByVal str2 As String,ByVal str3 As String) As String
Dim strAllString As String
strAllString = str1 + str2 + str3
Return strAllString
End Function
End Class
D层:
''' <summary>
''' 组合查询
''' </summary>
'''
''' <returns></returns>
''' <remarks></remarks>
Function CheckWorkLogInfo(ByValstrCombinationsql As String) As Datatable
Dim strsql As String ="select OperatorID as教师,OperatorLevel as 级别,logonDate as 登录日期,logonTime as 登录时间,logoutDate as注销日期,logoutTime as 注销时间,OperatorUseComputer as 机器名 from WorkLog_Info where" + strCombinationsql‘SQL语句
Dim cmd As sqlCommand = NewsqlCommand(strsql,conn)’连接数据库
Dim sqlda As NewsqlDataAdapter‘建一个sqlDataAdapter
Dim ds As New DataSet’定义一个新的DataSet
Dim dt As NewDatatable‘定义一个新的Datatable
Try
conn.open()’打开数据库
sqlda.SelectCommand = cmd‘把cmd传给da
sqlda.Fill(dt)’填充dt
Return dt
Catch ex As Exception
Return dt
Finally
cmd.dispose()
conn.Close()
End Try
End Function
以上是内存溢出为你收集整理的VB.NET机房收费系统——组合查询全部内容,希望文章能够帮你解决VB.NET机房收费系统——组合查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)