在VB.NET机房收费系统个人版的过程中,又遇到了组合查询了。机房收费系统的组合查询是所有组合查询类型中相对比较繁琐和困难的。当然,在.NET的组合查询中,我用的是拼接字符串。
下面以机房收费系统中“学生基本信息维护”为例,讲一下我是如何实现组合查询的
这是“学生基本信息维护”的界面:
我先说一下解决的思路:因为是用三层的思想,所以我们就需要考虑解决的方法和各个层应该放什么内容。我 用的方法是拼接字符串,所以首先U层:要在U层里面定义一个函数,实现文本框里面中英文的转换;然后,定义一个字符串,根据组合关系框的不同情况依次拼写字符串——
Public Class FormUI_ProtectStudentInfo '退出按钮 Private Sub btnCancel_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnCancel.Click FormUI_Main.Show() Me.HIDe() End Sub '查询按钮 Private Sub btnquery_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnquery.Click '清空DataGrIDVIEw控件的内容 DataGrIDVIEw1.DataSource = vbNull '字段名不允许为空 If cmbFIEldBox1.Text.Trim() = "" Then MessageBox.Show("对不起,字段名不允许为空,请您选择字段名","信息提示",MessageBoxbuttons.OK,MessageBoxIcon.Warning) Exit Sub End If ' *** 作符不允许为空 If cmbOperateBox1.Text.Trim() = "" Then MessageBox.Show("对不起, *** 作符不允许为空,请您选择 *** 作符",MessageBoxIcon.Warning) Exit Sub End If '要查询的内容不允许为空 If txtqueryContent1.Text.Trim() = "" Then MessageBox.Show("对不起,请您输入要查询的内容,要查询的内容不允许为空",MessageBoxIcon.Warning) Exit Sub End If '拼接字符串 Dim sqlstring As String = nothing '当组合关系框均为空时 If cmbConbineBox1.Text = "" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text '当第一个组合关系框为“或”第二个框为空时 ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text Or GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox2.Text + txtqueryContent2.Text '当第一个组合关系框为“与”第二个框为空时 ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox2.Text + txtqueryContent1.Text And GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox2.Text + txtqueryContent2.Text '当第一个框为“或”,第二个框为“或”时 ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "或" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text Or GetEnglish(cmbFIEldBox2.Text) + cmbOperateBox2.Text + txtqueryContent2.Text Or GetEnglish(cmbFIEldBox3.Text) + cmdOperateBox3.Text + txtqueryContent3.Text '当第一个框为“与”第二个框为“与”时 ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "与" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text And GetEnglish(cmbFIEldBox2.Text) + cmbOperateBox2.Text + txtqueryContent2.Text And GetEnglish(cmbFIEldBox3.Text) + cmdOperateBox3.Text + txtqueryContent3.Text '当第一个框为“或”第二个框为“与” ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "与" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text Or GetEnglish(cmbFIEldBox2.Text) + cmbOperateBox2.Text + txtqueryContent2.Text And GetEnglish(cmbFIEldBox3.Text) + cmdOperateBox3.Text + txtqueryContent3.Text '当第一个框为“与”第二个框为“或” ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "或" Then sqlstring = GetEnglish(cmbFIEldBox1.Text) + cmbOperateBox1.Text + txtqueryContent1.Text And GetEnglish(cmbFIEldBox2.Text) + cmbOperateBox2.Text + txtqueryContent2.Text Or GetEnglish(cmbFIEldBox3.Text) + cmdOperateBox3.Text + txtqueryContent3.Text End If '定义一个B层对象 Dim uB As New BLL.BLL_ProtectStudentInfo Dim a As Datatable a = uB.query_st(sqlstring) If a.Rows.Count() > 0 Then DataGrIDVIEw1.DataSource = a Else MessageBox.Show("没有记录",MessageBoxIcon.information) End If End Sub Public Function GetEnglish(ByVal strControl As String) As String Select Case (strControl) Case "卡号" Return "card_no" Case "学号" Return "student_ID" Case "性别" Return "st_sex" Case "姓名" Return "st_name" Case "年级" Return "st_grade" Case "院系" Return "st_department" Case "班级" Return "st_class" Case Else Return "" End Select End FunctionEnd Class
以上便是U层的情况,当然B层很简单了,只要定义一个函数,返回D层的查询结果就可以了,记住,在这个过程中,传递的是我们自己定义的拼接的字符串哦
Public Class BLL_ProtectStudentInfo
Public Function query_st(ByVal sqlstring As String) As Datatable '定义一个D层对象 Dim bD As New DAL.DAL_ProtectStudentInfo Dim dt As New Datatable dt = bD.query_ST(sqlstring) Return dt End FunctionEnd Class
D层就是根据所拼接的字符串,一个SQL语句便搞定啦
imports System.Data.sqlClIEntPublic Class DAL_ProtectStudentInfo '数据库连接语句 Dim str As String = "server=.;database=Computer room Charging System;integrated security=sspi" Dim conn As sqlClIEnt.sqlConnection '构造函数,建立连接,打开数据库 Public Sub New() conn = New sqlClIEnt.sqlConnection conn.ConnectionString = str conn.open() End Sub '定义一个函数 Public Function query_ST(ByVal sqlstring As String) As Datatable 'SQL语句 Dim sqlstr As String sqlstr = "select * from Student where " + sqlstring '执行 Dim myAdapter As sqlDataAdapter = New sqlDataAdapter(sqlstr,conn) Dim mytable As New Datatable() myAdapter.Fill(mytable) Return mytable End FunctionEnd Class总结
以上是内存溢出为你收集整理的机房收费系统个人版——VB.NET组合查询全部内容,希望文章能够帮你解决机房收费系统个人版——VB.NET组合查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)