[置顶] VB.NET+三层 机房收费系统之组合查询

时间:2022-02-10 06:18:38

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

 步骤:

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

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

 (3)、建立D层,D层中我调用的是视图,应为我把学生表和卡表分开了(这个不打紧),如果你可以只查询一张表,直接用查询表就行了;如果用的数据不单单在一张表上的时候,可以试试用视图,很简单的。

 (4)、建立U层,U层中主要是检查格式是否正确(主要是有没有空的,在代码中有详细说明,看代码中的注释就明白了)。

  三层设计:

  U层设计:

  这里我为什么先写U层呢?不应该先写实体(E)层吗?其实我就是先写的U层,因为我也不知道,我用到了那些实体,它不光光包含了数据库中的表的字段,还有一些其他的参数。

  U层的界面设计:

[置顶]        VB.NET+三层 机房收费系统之组合查询

  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:建立一个视图,直接调就行了。