标签:pop amp 赋值 数组 ase share ext import blog
关系组合查询已经用去了4天的时间。每天都在痛苦中煎熬,绞尽脑汁,一句代码都要瞪大眼睛看好长时间,有时候。由于两句话颠倒了。就nothing了;有时候,由于table如何可以转换成实体类型。将自己困住了。一想就是半天。状况不断呀。看了非常多师哥师姐们的代码,他们分享着自己的代码。为了给大家一点东西,给拿出自己给大家分享。期望大家能给点意见。
(1)、首先建立实体,实体是用来存储变量的。
(2)、建立B层,B层除了有调用D层的函数,同一时候,它也有将汉字转换为sql中的字段的功能。
(3)、建立D层。D层中我调用的是视图,应为我把学生表和卡表分开了(这个不打紧),假设你能够仅仅查询一张表。直接用查询表即可了;假设用的数据不单单在一张表上的时候。能够试试用视图。非常easy的。
(4)、建立U层,U层中主要是检查格式是否正确(主要是有没有空的,在代码中有具体说明。看代码中的凝视就明确了)。
U层设计:
这里我为什么先写U层呢?不应该先写实体(E)层吗?事实上我就是先写的U层。由于我也不知道,我用到了那些实体。它不光光包括了数据库中的表的字段,另一些其它的參数。
U层的界面设计:
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
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
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:建立一个视图,直接调即可了。
标签:pop amp 赋值 数组 ase share ext import blog
原文地址:http://www.cnblogs.com/yxysuanfa/p/7152099.html