您好,欢迎访问一九零五行业门户网

存储过程---留住你的美丽(下)

继上篇博客,存储过程---留住你的美丽(上),接着叨叨,美丽是留住了,如何让美丽持久惊艳别人的眼球,我们就要开动我们的小脑袋,转一转,想一想,一如上篇博客中所展示的代码一样,需要传十一个参数,各个层之间都需要重复写,键盘累了,坏了怎么办nie?还
继上篇博客,存储过程---留住你的美丽(上),接着叨叨,美丽是留住了,如何让美丽持久惊艳别人的眼球,我们就要开动我们的小脑袋,转一转,想一想,一如上篇博客中所展示的代码一样,需要传十一个参数,各个层之间都需要重复写,键盘累了,坏了怎么办nie?还要重新上京东买,咳咳咳,言归正传,依旧结合机房收费来说,如何让存储过程美丽持久,流年过往,经年不忘......
在机房收费系统中,涉及三个组合查询,分别是学生基本信息维护、学生上机状态查看、操作员工作记录;如何让三个组合查询使用同一个存储过程,就需要我们转转小小的脑袋,几经周折,发现,我们可以把表名当成一个参数进行传递,这样,用到不同表的时候,我们只需要变更表名即可,存储过程写法如下所示:
use [machineroomchargesystem]go/****** object: storedprocedure [dbo].[proc_groupquery] script date: 08/01/2014 19:24:15 ******/set ansi_nulls ongoset quoted_identifier ongo-- =============================================-- author: -- create date: -- description: -- =============================================alter procedure [dbo].[proc_groupquery] -- add the parameters for the stored procedure here @cbofielda varchar(10), --字段名a @cbooperatora varchar(10), --操作符a @txtcontenta varchar(10), --要查询的内容a @cborelationa varchar(10), --组合关系a @cbofieldb varchar(10), --字段名b @cbooperatorb varchar(10), --操作符b @txtcontentb varchar(10), --要查询的内容b @cborelationb varchar(10), --组合关系b @cbofieldc varchar(11), --字段c @cbooperatorc varchar(10), --操作符c @txtcontentc varchar(10), --要查询的内容c @tablename varchar(50) --要查询的视图名或表面名 as declare @strtext varchar(500)--临时存放sql语句 --char(32)是空格,char(39)单引号begin -- set nocount on added to prevent extra result sets from -- interfering with select statements. set @strtext ='select * from '+@tablename+' where status !='+char(39)+'正常上机'+char(39)+' and status !='+char(39)+'正在值班'+char(39)+' and'+char(32) +@cbofielda+@cbooperatora+char(39)+@txtcontenta +char(39) if @cborelationa!='' begin set @strtext=@strtext+@cborelationa+char(32)+ @cbofieldb+@cbooperatorb+char(39 )+@txtcontentb+char(39) end if @cborelationb!='' begin set @strtext=@strtext+@cborelationb+char(32)+ @cbofieldc+@cbooperatorc+char(39)+@txtcontentc+char(39) end execute(@strtext) end
e---实体层写法如下:'**********************************************'文 件 名: groupqueryentity'命名空间: entity'内 容:'功 能: 组合查询'文件关系:'作 者:丁国华'小 组:宝贝计划'生成日期: 2014/7/31 21:33:57'版本号:v2.0'修改日志:'版权说明:'**********************************************public class groupqueryentity ''' ''' 组合查询用到的一系列实体 ''' ''' private _cbofielda as string public property cbofielda() as string get return _cbofielda end get set(value as string) _cbofielda = value end set end property private _cbooperatora as string public property cbooperatora() as string get return _cbooperatora end get set(value as string) _cbooperatora = value end set end property private _txtcontenta as string public property txtcontenta() as string get return _txtcontenta end get set(value as string) _txtcontenta = value end set end property private _cborelationa as string public property cborelationa() as string get return _cborelationa end get set(value as string) _cborelationa = value end set end property private _cbofieldb as string public property cbofieldb() as string get return _cbofieldb end get set(value as string) _cbofieldb = value end set end property private _cbooperatorb as string public property cbooperatorb() as string get return _cbooperatorb end get set(value as string) _cbooperatorb = value end set end property private _txtcontentb as string public property txtcontentb() as string get return _txtcontentb end get set(value as string) _txtcontentb = value end set end property private _cborelationb as string public property cborelationb() as string get return _cborelationb end get set(value as string) _cborelationb = value end set end property private _cbofieldc as string public property cbofieldc() as string get return _cbofieldc end get set(value as string) _cbofieldc = value end set end property private _cbooperatorc as string public property cbooperatorc() as string get return _cbooperatorc end get set(value as string) _cbooperatorc = value end set end property private _txtcontentc as string public property txtcontentc() as string get return _txtcontentc end get set(value as string) _txtcontentc = value end set end property private _tablename as string public property tablename() as string get return _tablename end get set(value as string) _tablename = value end set end propertyend class
d层如下:就学生上机状态查询来说:'**********************************************'文 件 名: t_linedal'命名空间: dal'内 容:'功 能:'文件关系:'作 者:丁国华'小 组:宝贝计划'生成日期: 2014/7/25 10:39:13'版本号:v2.0'修改日志:'版权说明:'********************************************** ''' ''' 组合查询-学生上机状态查看 ''' ''' ''' ''' public function querystatus(engroupquery as entity.groupqueryentity) as list(of entity.lineentity) implements iline.querystatus dim strtext as string = proc_groupquery '从存储过程里面查询相应信息 dim cmdtype as string = commandtype.storedprocedure '命令类型 dim parameter as sqlparameter() '传参 parameter = {new sqlparameter(@cbofielda, engroupquery.cbofielda), new sqlparameter(@cbooperatora, engroupquery.cbooperatora), new sqlparameter(@txtcontenta, engroupquery.txtcontenta), new sqlparameter(@cborelationa, engroupquery.cborelationa), new sqlparameter(@cbofieldb, engroupquery.cbofieldb), new sqlparameter(@cbooperatorb, engroupquery.cbooperatorb), new sqlparameter(@txtcontentb, engroupquery.txtcontentb), new sqlparameter(@cborelationb, engroupquery.cborelationb), new sqlparameter(@cbofieldc, engroupquery.cbofieldc), new sqlparameter(@cbooperatorc, engroupquery.cbooperatorc), new sqlparameter(@txtcontentc, engroupquery.txtcontentc), new sqlparameter(@tablename, engroupquery.tablename)} dim sqlhelper as new sqlhelper dim dt as new datatable dim mylist as list(of entity.lineentity) dt = sqlhelper.executereadertable(strtext, cmdtype, parameter) mylist = entityhelper.converttolist(of entity.lineentity)(dt) return mylist end functionend class
对实体进行封装,把先前需要传递的十一个参数转换成现在只需要传递一个实体即可,还有一个细节问题需要注意的是,在存储过程里面sql语句where后面的条件该如何写nie?就学生查看上机记录和操作员工作记录来说,第一个组合查询涉及到的状态有正常上机,正常下机,强制下机;第二个组合查询涉及到的状态有正在值班和下班两种状态,我们的第一个组合查询的业务需求是查出来的信息是状态不等于正常上机的相关信息,第二个组合查询的业务需求是状态不等于正在值班的相关信息,那么在where后面的条件到底是用and连接还是用or连接,小伙伴们都知道and的意思是和,连词,连接两个并列结构,or是或者的意思,整到这里,我就开始整蒙圈了,到底是用or还是用and呢?开始我的第一反应用的是or,要么执行这个,要么执行那个,别着急,让我们用实话说话,焦点访谈,呵呵,开个玩笑,在sql中新建查询,看看用and连接,会发生什么好玩儿的事儿呢?
 接着,用or连接:
进过对比,我们发现,第一个用and连接,查询出来的信息的状态包括正常上机的和正常下机的,但是根据机房收费系统的业务需求,需要我们查询出来的信息是正常下机的,显然第二个才是我们所需要的信息,聪明的读者,明白了么?
对比我们之前学习过的vb中的函数过程和我们的存储过程,有什么不一样的地方呢?存储过程不一定非要有返回值数据库中的函数一定有返回值;存储过程在前台语言可以直接调用 而函数一般要有sql语句做为载体;用户自定义函数在处理同一数据行中的各个字段时,特别方便有用。虽然这里使用存储过程也能达到查询目的,但是显然没有使用函数方便。而且,即使使用存储过程也无法处理select查询中的同一数据行中的各个字段的运算。因为存储过程不返回值,使用时只能单独调用;而函数却能出现在能放置表达式的任何位置。第二版机房收费系统,未完,待续......
其它类似信息

推荐信息