关系组合查询已经用去了4天的时间。每天都在痛苦中煎熬,绞尽脑汁,一句代码都要瞪大眼睛看好长时间,有时候。由于两句话颠倒了。就nothing了;有时候,由于table如何可以转换成实体类型。将自己困住了。一想就是半天。状况不断呀。看了非常多师哥师姐们的代码,他们分享着自己的代码。为了给大家一点东西,给拿出自己给大家分享。期望大家能给点意见。

步骤:

(1)、首先建立实体,实体是用来存储变量的。

(2)、建立B层,B层除了有调用D层的函数,同一时候,它也有将汉字转换为sql中的字段的功能。

(3)、建立D层。D层中我调用的是视图,应为我把学生表和卡表分开了(这个不打紧),假设你能够仅仅查询一张表。直接用查询表即可了;假设用的数据不单单在一张表上的时候。能够试试用视图。非常easy的。

(4)、建立U层,U层中主要是检查格式是否正确(主要是有没有空的,在代码中有具体说明。看代码中的凝视就明确了)。

三层设计:

U层设计:

这里我为什么先写U层呢?不应该先写实体(E)层吗?事实上我就是先写的U层。由于我也不知道,我用到了那些实体。它不光光包括了数据库中的表的字段,另一些其它的參数。

U层的界面设计:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl1bXV4aWEwOTIx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

U层的代码:

Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class FrmOperStuInfo Private Sub btnQurry_Click(sender As Object, e As EventArgs) Handles btnQurry.Click
'----------------------------------------------------------------
'第一行查询不能为空。党第一行为空的话,后面的条件都不能有内容。
'当第一个组合框不为空是,第二行不能为空。同一时候,假设第一个组合框为空时,第二个组合框不能有内容
'当第二个组合框不为空时。第三行不能为空。 '----------------------------------------------------------------- Dim QueryStudent As New Entity.QueryStudentEntity '这个实体是用来传实体的
Dim QueryStudentBLL As New BLL.QurryStudentBLL
Dim controlArray(2) As Control
Dim table As System.Data.DataTable '接收实体的,但用的不是entity类型的。传回的是datatable,由于dataGirdView用的会比較舒服
Dim sqlstring As String 'sql的查询语句
Dim relation1 As String
Dim relation2 As String Try
controlArray(0) = combofield1 '第一行
controlArray(1) = Combochar1
controlArray(2) = TextBox1
QueryStudent.Field1 = QueryStudentBLL.ChangeField(combofield1.Text)
sqlstring = "SELECT * FROM V_QueryStudent WHERE " & QueryStudent.Field1 & Combochar1.Text & "'" & TextBox1.Text & "'" '当仅仅有第一行有东西的时候 If CommonFunction.IsSomeEmptyText(controlArray) Then '输入为空
Exit Sub
End If If Comborela1.Text.Trim <> "" Then '第一个组合框不为空的时候
controlArray(0) = Combofield2 '第二行
controlArray(1) = Combochar2
controlArray(2) = TextBox2
QueryStudent.Field2 = QueryStudentBLL.ChangeField(Combofield2.Text)
relation1 = QueryStudentBLL.ChangeRelation(Comborela1.Text)
sqlstring = sqlstring & " " & relation1 & " " & QueryStudent.Field2 & Combochar2.Text & TextBox2.Text '当仅仅有第一行有东西的时候 If CommonFunction.IsSomeEmptyText(controlArray) Then
Exit Sub
End If If Comborela2.Text.Trim <> "" Then '第二个组合框不为空的时候。第三行也一定不能为空
controlArray(0) = Combofield3 '第二行
controlArray(1) = Combochar3
controlArray(2) = TextBox3
QueryStudent.Field3 = QueryStudentBLL.ChangeField(Combofield3.Text)
relation2 = QueryStudentBLL.ChangeRelation(Comborela2.Text)
sqlstring = sqlstring & " " & relation2 & " " & QueryStudent.Field3 & Combochar3.Text & TextBox3.Text '当仅仅有第一行有东西的时候 If CommonFunction.IsSomeEmptyText(controlArray) Then
Exit Sub
End If
End If
Else
Comborela2.Text = "" '第一个组合框为空的时候。第二个组合框一定为空
End If QueryStudent.Field1 = combofield1.Text '往实体中传參用的
QueryStudent.Field2 = Combofield2.Text
QueryStudent.Field3 = Combofield3.Text QueryStudent.Char1 = Combochar1.Text
QueryStudent.Char2 = Combochar2.Text
QueryStudent.Char3 = Combochar3.Text QueryStudent.Relation1 = Comborela1.Text
QueryStudent.Relation2 = Comborela2.Text QueryStudent.Content1 = TextBox1.Text
QueryStudent.Content2 = TextBox2.Text
QueryStudent.Content3 = TextBox3.Text
QueryStudent.sqlstring = sqlstring '将字符串也要传给它 table = QueryStudentBLL.QueryStudent(QueryStudent) '接收返回值的table类型的 DataGridView1.DataSource = table '绑定 Catch ex As Exception
MsgBox(ex.Message) End Try End Sub

公共函数:

Public Class CommonFunction
Public Shared Function IsAllEmptyText(ByVal frm As Form) As Boolean
Dim control As New Control For Each control In frm.Controls '遍历窗口中全部的控件
'MsgBox(frm.Controls.Count)
If TypeOf control Is TextBox Then '推断控件是不是文本框
If control.Text.Trim = "" Then '推断文本框内容是否为空
MsgBox(control.Tag.ToString + "不能为空。", vbOKOnly, "温馨提示")
control.Focus()
Return True
Exit Function
End If
ElseIf TypeOf control Is ComboBox Then '推断控件是不是组合框
If control.Text.Trim = "" Then
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
Return True
Exit Function
End If
End If
Next Return False
End Function ''' 推断控件数组中的控件的Text属性是否为空,有空时返回true Public Shared Function IsSomeEmptyText(ByVal arrayControl() As Control) As Boolean
Dim control As New Control For Each control In arrayControl '遍历数组中全部元素
If TypeOf control Is TextBox Then '推断控件是不是文本框
If control.Text.Trim = "" Then '推断文本框内容是否为空
'MsgBox(control.Tag.ToString + "不能为空。", vbOKOnly, "温馨提示")
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
control.Focus()
Return True
Exit Function
End If
ElseIf TypeOf control Is ComboBox Then '推断控件是不是组合框
If control.Text.Trim = "" Then
'MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
MsgBox("不能为空! ", vbOKOnly, "温馨提示") Return True
Exit Function
End If
End If
Next Return False
End Function
End Class

上面是一个窗口,是用来调用B层,以下是一个类。是用来推断格式(是否有本不该为空的控件)通俗的讲。

E层的代码:

Public Class QurryStudentBLL
Public Function ChangeObject(ByVal qurryStudent As Entity.QueryStudentEntity) As Entity.QueryStudentEntity
qurryStudent.Field1 = ChangeField(qurryStudent.Field1)
qurryStudent.Field2 = ChangeField(qurryStudent.Field2)
qurryStudent.Field3 = ChangeField(qurryStudent.Field3)
'这里的char 是不须要转换的
qurryStudent.Relation1 = ChangeRelation(qurryStudent.Relation1)
qurryStudent.Relation2 = ChangeRelation(qurryStudent.Relation2)
Return qurryStudent
End Function Public Function ChangeField(ByVal strField As String) As String '转换字段
Dim strFields As String = ""
Select Case strField
Case "卡号"
strFields = "CardNo"
Case "学号"
strFields = "StudentNO"
Case "姓名"
strFields = "StudentName"
Case "性别"
strFields = "Sex"
Case "系别"
strFields = "Department"
Case "年级"
strFields = "Grade"
Case "班级"
strFields = "Class"
End Select
Return strFields
End Function Public Function ChangeRelation(ByVal strRelation As String) As String '转换组合符
Dim strRelations As String = ""
Select Case strRelation
Case "与"
strRelations = "AND"
Case "或"
strRelations = "OR"
Case ""
strRelations = "" '这里也能够给它赋一个空值
End Select
Return strRelations
End Function Public Function QueryStudent(ByVal QueryStudentBLL As Entity.QueryStudentEntity) As System.Data.DataTable
Dim queryStudentDAL As New DAL.StudentDAL
Dim queryStudents As New System.Data.DataTable queryStudents = queryStudentDAL.QueryStudent(QueryStudentBLL)
Return queryStudents End Function
End Class

通过U层,就能够知道,B层里面须要转换的字符,以及调用D层的函数。

    Function QueryStudent(ByVal queryStudent1 As Entity.QueryStudentEntity) As System.Data.DataTable
Dim conn As SqlConnection = New SqlConnection(SqlUtil.connstring()) '定义连接打开数据库,这里也有还有一种方法写
Dim sql As String
sql = queryStudent1.sqlstring
Dim cmd As SqlCommand = New SqlCommand(sql, conn) '定义数据库命令
'cmd.CommandText = sql '存储过程
'cmd.CommandType = CommandType.Text
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter(cmd) Dim table As DataTable = New DataTable() '要返回的是datatable类型的 Try
conn.Open()
myAdapter.Fill(table) 'fill是用来将Adapter查询到的视图,放到table里面 Catch ex As Exception
MsgBox(ex.Message)
End Try Return table End Function
End Class

好的。到了这里就整个都写完了。

注意:

(1)、DataTable这样转换为实体类型?答:不须要一个一个赋值,真的非常麻烦。直接这样就能够了//table = QueryStudentBLL.QueryStudent(QueryStudent) '接收返回值的table类型的//.

(2)、假设我在SQL中用来了两个表查询,是不是建立两个实体,两个B层呀?答:不须要,方法1:建立一个实体,将两个表所用到的字段放到里面。方法2:建立一个视图,直接调即可了。

VB.NET+三层 机房收费系统之组合查询的更多相关文章

  1. vb.net机房收费系统之组合查询

    我个人一直认为,组合查询是机房收费系统的一个难点,尤其是用到三层之后,如果要为组合查询中的每一个查询建立一个显然是太麻烦了. 下面介绍一下我的方法,对大家起个参考作用. 我将该表中可输入的内容定义为一 ...

  2. VB.NET版机房收费系统---七仙女之系统登录

    VB.NET第一版机房收费系统,告一段落,验收的时候.问题也是大大的存在,没实用上设计模式,什么触发器.存储过程,都没实用上.看看其她小伙伴的,七层实现登录?那是什么东东,相比較我的三层而言,多了两倍 ...

  3. VB.NET版机房收费系统---导出Excel表格

    datagridview,翻译成中文的意思是数据表格显示,使用DataGridView控件,能够显示和编辑来自不同类型的数据源的表格,将数据绑定到DataGridView控件很easy和直观,大多数情 ...

  4. VB.NET版机房收费系统---异常处理

    异常处理,英文名为Exceptional Handling, 那时年少,还记得那年一起学习过的VB6.0的时候,常常使用ONError的错误语句.与传统VB6.0中的OnError语句相比.NET平台 ...

  5. VB.net版机房收费系统——结账功能实现(调错与优化)

    调错部分 上一篇博客<VB.net版机房收费系统--结账功能实现(代码部分>说的是结账功能的实现,亮出了代码.是在为这篇博客做铺垫.尽管结账功能代码是借鉴的巨人的博客.可是自己比着葫芦画瓢 ...

  6. VB.NET版机房收费系统---组合查询

    查询的意思就是查找,寻找,指在某一个或几个地方找出自己所要的信息,假如我想搜索一下我自己写的博客,名字叫做初雪之恋,我在百度的搜索框中输入丁国华三个字,会有怎样的惊喜等着我? 啊哦,这个信息并不是我想 ...

  7. VB.NET版机房收费系统---外观层如何写

    外观设计模式,<大话设计模式>第103页详细讲解,不记得这块知识的小伙伴可以翻阅翻阅,看过设计模式,敲过书上的例子,只是学习的第一步,接着,如果在我们的项目中灵活应用,把设计模式用出花儿来 ...

  8. VB.NET版机房收费系统---报表

    报表,即报告情况的表格,简单的说:报表就是用表格.图表等格式来动态显示数据,可以用公式表示为:"报表 = 多样的格式 + 动态的数据". 在没有计算机以前,人们利用纸和笔来记录数据 ...

  9. VB.NET版机房收费系统---外观层怎样写

    外观设计模式.<大话设计模式>第103页具体解说,不记得这块知识的小伙伴能够翻阅翻阅,看过设计模式,敲过书上的样例,仅仅是学习的第一步,接着,假设在我们的项目中灵活应用,把设计模式用出花儿 ...

随机推荐

  1. GridSearchCV 与 RandomizedSearchCV 调参

    GridSearchCV    GridSearchCV的名字其实可以拆分为两部分,GridSearch和CV,即网格搜索和交叉验证. 这两个概念都比较好理解,网格搜索,搜索的是参数,即在指定的参数范 ...

  2. 第3节 mapreduce高级:12、mapreduce相关的参数调整

    5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...

  3. NET SignaiR 实现消息的推送,并使用Push.js实现通知

    一.使用背景 1. SignalR是什么? ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程.实时 Web 功能是指 ...

  4. socket编程(Java实现)

    主要是前段时间学习的网络知识的基于TCP与UDP编程,Java实现简单的大小写字母的转化,该文主要参考: https://blog.csdn.net/yjp19871013/article/detai ...

  5. torch.nn.Embedding理解

    Pytorch官网的解释是:一个保存了固定字典和大小的简单查找表.这个模块常用来保存词嵌入和用下标检索它们.模块的输入是一个下标的列表,输出是对应的词嵌入. torch.nn.Embedding(nu ...

  6. extjs传递参数

    以前我是这么传的: 先获取表Form,再通过表找组件,然后用gradeCode=0&groupCode=1传 现在只需要先获得表,然后通过form.getValues()取得所有的值,再使用E ...

  7. node 转二进制 图片

    'use strict';const Service = require('egg').Service;const fs = require('fs');const path = require('p ...

  8. 禁止浏览器static files缓存篇

    由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. Meta法 目前在chrome调试还没有遇到问题,好用!此方法假设浏览器是个好人!很听我们的话! <meta http ...

  9. 【06】对AJAX的总结(转)

    对AJAX的总结   通过前面对 AJAX 的讲解,我们可以将 AJAX 请求分成以下几个步骤: 创建 XMLHttpRequest 对象: 设置事件处理函数,处理返回的数据: 初始化并发送请求. 可 ...

  10. angular(转)

    学习之前可以看看 知乎上讨论angularjs优缺点 帮你选择框架的网站 同类主流框架对比 教程 angularjs在慕课网 angularjs在51cto angularjs在图灵社区 社区 Ang ...