快活林资源网 Design By www.csstdc.com
而在对用户进行查询时,也可能会使用到多种条件的查询方式,如通过工号查询、通过姓名查询、通过性别查询、通过学历查询等。也有可能会通过多种条件的组合查询,如查学历是大专的女员工等。
对于这种查询情况,通常的作法是让用户输入查询条件,再进行SQL语句组合来进行查询。如让用户输入工号、姓名等,单击提交按钮之后,在后台获得这些信息,如以下代码所示:
复制代码 代码如下:
//设置查询语句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用户名不为空则添加查询条件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性别不为空则添加查询条件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在创建完SQL语句之后,执行该语句获得查询结果。
这种是使用得最多并且是最不安全的方法,因为这是最容易让别人SQL注入攻击的一个方式。
如果想要避免SQL注入攻击,可以将查询语句写在存储过程中,然后使用SqlParameter将参数传递给存储过程,但是,一个多条件查询的存储过程需要怎么写呢?
其实,这个存储过程并不难,可以使用以下方式:
复制代码 代码如下:
CREATE PROCEDURE [dbo].[UserCheck]
@UserId varchar(50) = null,
@UserName varchar(20) = null,
@RealName varchar(20) = null,
@Sex bit = null,
@JobTitle varchar(50) = null,
@Organ varchar(50) = null,
@IDCardType smallint = null,
@IDCard varchar(50) = null,
@Mobile varchar(50) = null
AS
BEGIN
select * from [user]
where UserId like case when @UserId is null then UserId else @UserId end
and UserName like case when @UserName is null then UserName else @UserName end
and RealName like case when @RealName is null then RealName else @RealName end
and Sex = case when @Sex is null then Sex else @Sex end
and JobTitle like case when @JobTitle is null then JobTitle else @JobTitle end
and Organ like case when @Organ is null then Organ else @Organ end
and IDCardType = case when @IDCardType is null then IDCardType else @IDCardType end
and IDCard like case when @IDCard is null then IDCard else @IDCard end
and Mobile like case when @Mobile is null then Mobile else @Mobile end
END
对于这种查询情况,通常的作法是让用户输入查询条件,再进行SQL语句组合来进行查询。如让用户输入工号、姓名等,单击提交按钮之后,在后台获得这些信息,如以下代码所示:
复制代码 代码如下:
//设置查询语句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用户名不为空则添加查询条件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性别不为空则添加查询条件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在创建完SQL语句之后,执行该语句获得查询结果。
这种是使用得最多并且是最不安全的方法,因为这是最容易让别人SQL注入攻击的一个方式。
如果想要避免SQL注入攻击,可以将查询语句写在存储过程中,然后使用SqlParameter将参数传递给存储过程,但是,一个多条件查询的存储过程需要怎么写呢?
其实,这个存储过程并不难,可以使用以下方式:
复制代码 代码如下:
CREATE PROCEDURE [dbo].[UserCheck]
@UserId varchar(50) = null,
@UserName varchar(20) = null,
@RealName varchar(20) = null,
@Sex bit = null,
@JobTitle varchar(50) = null,
@Organ varchar(50) = null,
@IDCardType smallint = null,
@IDCard varchar(50) = null,
@Mobile varchar(50) = null
AS
BEGIN
select * from [user]
where UserId like case when @UserId is null then UserId else @UserId end
and UserName like case when @UserName is null then UserName else @UserName end
and RealName like case when @RealName is null then RealName else @RealName end
and Sex = case when @Sex is null then Sex else @Sex end
and JobTitle like case when @JobTitle is null then JobTitle else @JobTitle end
and Organ like case when @Organ is null then Organ else @Organ end
and IDCardType = case when @IDCardType is null then IDCardType else @IDCardType end
and IDCard like case when @IDCard is null then IDCard else @IDCard end
and Mobile like case when @Mobile is null then Mobile else @Mobile end
END
快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com
暂无评论...
更新日志
2024年04月24日
2024年04月24日
- 汪峰 许巍《巍峰凛凛·实力比拼》3CD[WAV分轨][2.2G]
- dnf白字红字黄字是什么意思
- dnf石化鞋怎么搭配
- dnf神之真谛称号属性
- 《天国拯救2》游戏特色介绍
- 《无感染区》开局建家探索心得
- 《圣剑传说Visions of Mana》灵器作用介绍
- 吴克群2023-今天很OK[何乐音乐][WAV+CUE]
- 邱兰芬.2002-台湾红歌100年·台语精选辑CD19【乡城】【WAV+CUE】
- 满江.1998-多变的海【SONY】【WAV+CUE】
- 赞达亚宣传R级新片《挑战者》 粉色裙装秀出纤细蛮腰
- 《老头环》经常被玩家跳过的BOSS:多个双人组上榜
- B社辐射3开发者曾遭死亡威胁:对方扬言割断其喉咙
- 原画壁纸及美图第117期,无水印可自取
- 原画壁纸及美图第118期,无水印可自取