带有存储过程的组合查询
2013-02-21 21:13
204 查看
组合查询从它的表面的意思我们知道是通过一个条件或者多个条件进行查询,我们先来看看组合查询的界面:
代码:
U层:
在U层调用一个判断控件是否为空的函数:
B层:
D层:
存储过程:
Sqlhellp:
总结:
1.存储过程的编写与使用,在刚开始学习数据的时候,只是知道有存储过程和事务,但是从来没有自己真正的动手去
写,刚开始着手的时候,感觉挺难的,难主要是难在不知道从哪里下手,刚开始写简单的存储过程还可以,当写组合
查询这块的存储过程就是错误百出了,而且还不知道是在哪里出现了错误,在网上查找了很多关于存储过程的内容,
捣鼓了几天,终于做出来了,那一刻感觉真的是特别好。
2.Sqlhelp的编写与使用,SQLHelp 知道它是在学习UML的时候,但是对它是挺好奇的,但是没有怎么去研究,这次
写机房收费系统的代码,在D层用到数据连接,不断的重复写着同样的语句,感觉特别的麻烦,就开始试着去写
SQLHelp,在网上找了写资料,我看着也都是迷迷糊糊的,看来看去,还不如我自己动手写呢,写完了,感觉不过就是
如此吧,SQLHelp 和VB版的模块差不多,对它我并不陌生。
3.对三层的应用,机房收费系统是用三层来做的,其实吧,用几层来做并重要,重要的是理解分层的思想,就像有些
判断不一定需要放在B层,也可以放在U层的。在做机房书费系统的时候,把三层运用得更加顺手了。
4.有时候感觉很难很难,都是自己在吓自己,遇到再难的问题,在我们面前还有一个巨人——百度,要相信自己。
代码:
U层:
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click Dim EntityWork As New CRCMEntity.CombinationqueryEntity Dim BllWork As New CRCMBLL.OperatorWorkRecordBLL '在只有一个条件的情况下.判断条件是否输入完整 If cmbRelation1.Text = "" Then Dim arrayCtl() As Control ReDim Preserve arrayCtl(2) arrayCtl(0) = cmbFiles1 arrayCtl(1) = cmbOperator1 arrayCtl(2) = txtCondition1 IsFrmControlEmpty.IsSomeEmpty(arrayCtl) End If '在有两个条件进行组合查询的时候判断条件是否输入完整 If cmbRelation1.Text <> "" Then Dim arrayCtl() As Control ReDim Preserve arrayCtl(6) arrayCtl(0) = cmbFiles1 arrayCtl(1) = cmbOperator1 arrayCtl(2) = txtCondition1 arrayCtl(3) = cmbRelation1 arrayCtl(4) = cmbFiles2 arrayCtl(5) = cmbOperator2 arrayCtl(6) = txtCondition2 IsFrmControlEmpty.IsSomeEmpty(arrayCtl) End If '在用三个条件进行组合查询的时候判断条件是否输入完整 If cmbRelation2.Text <> "" Then IsFrmControlEmpty.IsAllEmpty(Me) End If '第一行字段名 EntityWork.Filed1 = cmbFiles1.Text '第一行操作符 EntityWork.Operater1 = cmbOperator1.Text '第一行条件 EntityWork.Condition1 = txtCondition1.Text '第一行组合关系 EntityWork.CombinationRelation1 = cmbRelation1.Text '第二行字段名 EntityWork.Filed2 = cmbFiles2.Text '第二行操作符 EntityWork.Operater2 = cmbOperator2.Text '第二行条件 EntityWork.Condition2 = txtCondition2.Text '第二行组合关系 EntityWork.CombinationRelation2 = cmbRelation2.Text '第三行字段名 EntityWork.Filed3 = cmbFiles3.Text '第三行操作符 EntityWork.Operater3 = cmbOperator3.Text '第三行条件 EntityWork.Condition3 = txtCondition3.Text dgvWorkRecord.DataSource = BllWork.WorkRecord(EntityWork) End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub End Class
在U层调用一个判断控件是否为空的函数:
'************************************************* '作者:唐欢 '小组: '说明:判断窗体界面的空间是否为空 '创建日期:2013/2/19 '版本号:V1.00 '**********************************************' Public Class IsFrmControlEmpty ''' <summary> '''判断窗体中所有的控件是否全部为空 ''' </summary> Public Shared Function IsAllEmpty(ByVal Frm As Form) As Boolean Dim control As New Control For Each Ctl As Control In Frm.Controls '变量窗体中的所有控件 If Ctl.GetType() Is GetType(TextBox) Then '判断控件类型是否为TextBox If Ctl.Text.Length = 0 Then '判断TextBox是否为空,返回True,否则返回False MessageBox.Show(String.Format("信息为填写完整,请把信息填写完整再保存!")) Ctl.Focus() Return True Exit Function End If ElseIf Ctl.GetType() Is GetType(ComboBox) Then '如果空间为Combobox, If Ctl.Text.Length = 0 Then '如果Combobox为空,返回True,否则返回False MsgBox(Ctl.Tag.ToString + "不能为空!", vbOK, "温馨小提示") Ctl.Focus() Return True Exit Function End If End If Next Return False End Function ''' <summary> '''判断窗体中部分控件是否为空 ''' </summary> Public Shared Function IsSomeEmpty(ByVal ArrayCtl() As Control) As Boolean Dim control As New Control For Each ctl As Control In ArrayCtl '遍历数组中所有元素 If ctl.GetType() Is GetType(TextBox) Then '如果控件类型为TextBox If ctl.Text.Length = 0 Then '如果输入框为空,返回True ,否则返回False MsgBox(ctl.Tag.ToString + "不能为空!", vbOK, "温馨小提示") ctl.Focus() '输入框获得焦点 Return True Exit Function End If ElseIf ctl.GetType() Is GetType(ComboBox) Then '判断控件类型是否为Combobox If ctl.Text.Length = 0 Then '如果Combobox的为空,返回True,否则返回False MsgBox(ctl.Tag.ToString + "不能为空!", vbOK, "温馨小提示") ctl.Focus() Return True Exit Function End If End If Next Return False End Function
B层:
''' <summary> ''' 查询工作记录 ''' </summary> Public Function WorkRecord(ByVal WorkHistory As CombinationqueryEntity) As DataTable Dim EntityWork As New CRCMEntity.CombinationqueryEntity Dim DalWork As New CRCMDAL.OperatorWorkRecordDAL '第一行字段名 EntityWork.Filed1 = WorkHistory.Filed1 '第一行操作符 EntityWork.Operater1 = WorkHistory.Operater1 '第一行条件 EntityWork.Condition1 = WorkHistory.Condition1 '第一行组合关系 EntityWork.CombinationRelation1 = WorkHistory.CombinationRelation1 '第二行字段名 EntityWork.Filed2 = WorkHistory.Filed2 '第二行操作符 EntityWork.Operater2 = WorkHistory.Operater2 '第二行条件 EntityWork.Condition2 = WorkHistory.Condition2 '第二行组合关系 EntityWork.CombinationRelation2 = WorkHistory.CombinationRelation2 '第三行字段名 EntityWork.Filed3 = WorkHistory.Filed3 '第三行操作符 EntityWork.Operater3 = WorkHistory.Operater3 '第三行条件 EntityWork.Condition3 = WorkHistory.Condition3 Dim table As DataTable '定义一个数据集 table = DalWork.SelectOperatorWorkRecord(EntityWork) Return table End Function
D层:
''' <summary> ''' 通过条件查找查询操作员工作记录 ''' </summary> Public Function SelectOperatorWorkRecord(ByVal WorkRecord As CombinationqueryEntity) As DataTable Dim sqlcmdstr As String = "PROC_CR_Select_OpertorWorkRecord" Dim paras As SqlParameter() = {New SqlParameter("@Field1", WorkRecord.Filed1), New SqlParameter("@Operation1", WorkRecord.Operater1), New SqlParameter("@Condition1", WorkRecord.Condition1), New SqlParameter("@Combinationrelations1", WorkRecord.CombinationRelation1), New SqlParameter("@Field2", WorkRecord.Filed2), New SqlParameter("@Operation2", WorkRecord.Operater2), New SqlParameter("@Condition2", WorkRecord.Condition2), New SqlParameter("@Combinationrelations2", WorkRecord.CombinationRelation2), New SqlParameter("@Field3", WorkRecord.Filed3), New SqlParameter("@Operation3", WorkRecord.Operater3), New SqlParameter("@Condition3", WorkRecord.Condition3) } Dim table As DataTable '定义一个数据集 table = SqlHelp.Sqlhelper.SelectInfo(sqlcmdstr, CommandType.StoredProcedure, paras) Return table End Function
存储过程:
-- ============================================= -- Author: 唐欢 -- Create date: 2013年2月15日 -- Description: 查询操作员工作记录 -- ============================================= ALTER PROCEDURE [dbo].[PROC_CR_Select_OpertorWorkRecord] --第一行 @Field1 varchar(40), --字段名 @Operation1 varchar(10),--操作符 @Condition1 char(50),--条件 @Combinationrelations1 char(10), --组合关系 --第二行 @Field2 varchar(40), --字段名 @Operation2 varchar(10),--操作符 @Condition2 char(50),--条件 @Combinationrelations2 char(10), --组合关系 --第三行 @Field3 varchar(40), --字段名 @Operation3 varchar(10),--操作符 @Condition3 char(50)--条件 AS declare @Sql varchar(400) declare @Sqlstr1 varchar(20) --第一行中的字段名对应表中字段名 declare @Sqlstr2 varchar(20) --第二行中的字段名对应表中字段名 declare @Sqlstr3 varchar(20) --第三行中的字段名对应表中字段名 declare @SqlRelations1 varchar(20) --表示第一行中的组合关系 declare @SqlRelations2 varchar(20) --表示第二行中的组合关系 BEGIN --表示第一行中的字段名用表中的字段名表示 IF (@Field1 ='教师') set @Sqlstr1 ='UserName' if (@Field1 ='登陆日期') set @Sqlstr1 ='UseDate' if (@Field1 ='登陆时间') set @Sqlstr1 ='UseTime' if (@Field1 ='注销日期') set @Sqlstr1 ='NouseDate' if (@Field1 ='注销时间') set @Sqlstr1 ='NouseTime' if (@Field1 ='机器名') set @Sqlstr1 ='RoomNo' --表示第二行中的字段名用表中的字段名表示 IF (@Field2 ='教师') set @Sqlstr2 ='UserName' if (@Field2 ='登陆日期') set @Sqlstr2 ='UseDate' if (@Field2 ='登陆时间') set @Sqlstr2 ='UseTime' if (@Field2 ='注销日期') set @Sqlstr2 ='NouseDate' if (@Field2 ='注销时间') set @Sqlstr2 ='NouseTime' if (@Field2 ='机器名') set @Sqlstr2 ='RoomNo' --表示第三行中的字段名用表中的字段名表示 IF (@Field3 ='教师') set @Sqlstr3 ='UserName' if (@Field3 ='登陆日期') set @Sqlstr3 ='UseDate' if (@Field3 ='登陆时间') set @Sqlstr3 ='UseTime' if (@Field3 ='注销日期') set @Sqlstr3 ='NouseDate' if (@Field3 ='注销时间') set @Sqlstr3 ='NouseTime' if (@Field3 ='机器名') set @Sqlstr3 ='RoomNo' --第一个组合关系 if (@Combinationrelations1='') set @SqlRelations1=null if (@Combinationrelations1='和') set @SqlRelations1='AND' if (@Combinationrelations1='或') set @SqlRelations1='OR' --第二个组合关系 if (@Combinationrelations2 ='') set @SqlRelations2=null if (@Combinationrelations2 ='和') set @SqlRelations2 ='AND' if (@Combinationrelations2 ='或') set @SqlRelations2 ='OR' set @Sql ='select UserName AS 用户名,UseDate as 登陆日期,UseTime as 登陆时间,NouseDate as 退出日期,NouseTime as 退出时间,RoomNo as 机器名 from WorkRecord where '+@Sqlstr1 ++@Operation1+char(39)+@Condition1 +CHAR(39) if (@SqlRelations1 is not null) set @Sql =@Sql +@SqlRelations1 +CHAR(32)+@Sqlstr2 +@Operation2 +CHAR(39)+@Condition2 +CHAR(39) if (@SqlRelations2 is not null) set @Sql =@Sql +@SqlRelations2 +CHAR(32)+@Sqlstr3 +@Operation3 +CHAR(39)+@Condition3 +CHAR(39) execute (@Sql) END
Sqlhellp:
''' <summary> '''带参数的查询 ''' </summary> Public Shared Function SelectInfo(ByVal CommandText As String, ByVal CommandType As CommandType, ByVal CommandParameters As SqlParameter()) As DataTable Dim sqlcn As New SqlConnection '定义数据库连接对象 sqlcn = GetConnect() sqlcn.Open() '打开数据库 Dim sqlcmd As SqlCommand '定义命令对象 sqlcmd = New SqlCommand(CommandText, sqlcn) Dim InfoAdaptor As SqlDataAdapter '定义适配器对象 Dim InfoDataset As New DataSet '定义一个数据集 InfoAdaptor = New SqlDataAdapter(sqlcmd) sqlcmd.CommandType = CommandType sqlcmd.Parameters.AddRange(CommandParameters) '添加参数 InfoAdaptor.Fill(InfoDataset) '填充适配器 Return InfoDataset.Tables(0) '返回数据集的表,从第一个开始索引 sqlcn.Close() End Function
总结:
1.存储过程的编写与使用,在刚开始学习数据的时候,只是知道有存储过程和事务,但是从来没有自己真正的动手去
写,刚开始着手的时候,感觉挺难的,难主要是难在不知道从哪里下手,刚开始写简单的存储过程还可以,当写组合
查询这块的存储过程就是错误百出了,而且还不知道是在哪里出现了错误,在网上查找了很多关于存储过程的内容,
捣鼓了几天,终于做出来了,那一刻感觉真的是特别好。
2.Sqlhelp的编写与使用,SQLHelp 知道它是在学习UML的时候,但是对它是挺好奇的,但是没有怎么去研究,这次
写机房收费系统的代码,在D层用到数据连接,不断的重复写着同样的语句,感觉特别的麻烦,就开始试着去写
SQLHelp,在网上找了写资料,我看着也都是迷迷糊糊的,看来看去,还不如我自己动手写呢,写完了,感觉不过就是
如此吧,SQLHelp 和VB版的模块差不多,对它我并不陌生。
3.对三层的应用,机房收费系统是用三层来做的,其实吧,用几层来做并重要,重要的是理解分层的思想,就像有些
判断不一定需要放在B层,也可以放在U层的。在做机房书费系统的时候,把三层运用得更加顺手了。
4.有时候感觉很难很难,都是自己在吓自己,遇到再难的问题,在我们面前还有一个巨人——百度,要相信自己。
相关文章推荐
- 疑问:关于组合查询时候的方法实现?能在存储过程中综合一下吗?
- 组合查询存储过程的实现
- 组合查询存储过程的实现
- 利用存储过程进行组合查询
- 机房重构——模板方法模式+单例模式+存储过程优化组合查询
- 佩特来项目经验小集合(2)___组合查询存储过程,报错 "varchar JBID='' 转换成数据类型 int 时失败"
- 存储过程实现组合查询
- 带有远程服务器,登录名和密码的存储过程 查询范例
- Sybase 动态改变存储过程里查询的数据库
- 通用分页显示查询存储过程
- 用exec调用带有output输出参数的存储过程
- sql查询所有存储过程内容-利用游标和临时表
- oracle之存储过程5-查询结果保存到变量中去
- [MSSQL]千万数量级分页存储过程,可支持多表查询,任意排序(来自百度文库)
- 如此高效通用的分页存储过程是带有sql注入漏洞的
- 存储过程查询 和导入导出存储过程
- 两个综合查询的SQL存储过程!
- SqlServer 多条件查询 [存储过程] 经典例子
- Sql Server 编写多条件查询语句的存储过程--包含语法错误的存储过程与正确存储过程
- mysql查询存储过程和函数、视图、触发器