VB.NET机房收费系统——组合查询

前端之家收集整理的这篇文章主要介绍了VB.NET机房收费系统——组合查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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" + strCombinationsqlsql语句

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

原文链接:https://www.f2er.com/vb/259621.html

猜你在找的VB相关文章