多條件查詢的程序
更新時(shí)間:2009年05月27日 01:45:38 作者:
在一個(gè)網(wǎng)站中,常常會(huì)使用到查詢功能。假設(shè)一個(gè)企業(yè)內(nèi)部網(wǎng)中,用戶信息里通常會(huì)涉及到工號(hào)、姓名、性別、學(xué)歷、職業(yè)、職稱、身份證號(hào)碼、手機(jī)號(hào)碼、座機(jī)號(hào)碼、傳真號(hào)碼、郵政編號(hào)、通訊地址等信息。
而在對(duì)用戶進(jìn)行查詢時(shí),也可能會(huì)使用到多種條件的查詢方式,如通過工號(hào)查詢、通過姓名查詢、通過性別查詢、通過學(xué)歷查詢等。也有可能會(huì)通過多種條件的組合查詢,如查學(xué)歷是大專的女員工等。
對(duì)于這種查詢情況,通常的作法是讓用戶輸入查詢條件,再進(jìn)行SQL語句組合來進(jìn)行查詢。如讓用戶輸入工號(hào)、姓名等,單擊提交按鈕之后,在后臺(tái)獲得這些信息,如以下代碼所示:
//設(shè)置查詢語句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用戶名不為空則添加查詢條件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性別不為空則添加查詢條件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在創(chuàng)建完SQL語句之后,執(zhí)行該語句獲得查詢結(jié)果。
這種是使用得最多并且是最不安全的方法,因?yàn)檫@是最容易讓別人SQL注入攻擊的一個(gè)方式。
如果想要避免SQL注入攻擊,可以將查詢語句寫在存儲(chǔ)過程中,然后使用SqlParameter將參數(shù)傳遞給存儲(chǔ)過程,但是,一個(gè)多條件查詢的存儲(chǔ)過程需要怎么寫呢?
其實(shí),這個(gè)存儲(chǔ)過程并不難,可以使用以下方式:
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
對(duì)于這種查詢情況,通常的作法是讓用戶輸入查詢條件,再進(jìn)行SQL語句組合來進(jìn)行查詢。如讓用戶輸入工號(hào)、姓名等,單擊提交按鈕之后,在后臺(tái)獲得這些信息,如以下代碼所示:
復(fù)制代碼 代碼如下:
//設(shè)置查詢語句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用戶名不為空則添加查詢條件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性別不為空則添加查詢條件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在創(chuàng)建完SQL語句之后,執(zhí)行該語句獲得查詢結(jié)果。
這種是使用得最多并且是最不安全的方法,因?yàn)檫@是最容易讓別人SQL注入攻擊的一個(gè)方式。
如果想要避免SQL注入攻擊,可以將查詢語句寫在存儲(chǔ)過程中,然后使用SqlParameter將參數(shù)傳遞給存儲(chǔ)過程,但是,一個(gè)多條件查詢的存儲(chǔ)過程需要怎么寫呢?
其實(shí),這個(gè)存儲(chǔ)過程并不難,可以使用以下方式:
復(fù)制代碼 代碼如下:
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
相關(guān)文章
windows安裝Neo4j圖數(shù)據(jù)庫的詳細(xì)過程
本文介紹了在Windows上安裝和配置Neo4j圖數(shù)據(jù)庫的步驟,包括安裝JavaSDK、解壓安裝Neo4j、配置環(huán)境變量、啟動(dòng)數(shù)據(jù)庫以及服務(wù)化啟動(dòng),感興趣的朋友一起看看吧2025-03-03
Navicat?Premium12進(jìn)行數(shù)據(jù)庫定期自動(dòng)備份的方法步驟
本文主要介紹了Navicat?Premium?12進(jìn)行數(shù)據(jù)庫定期自動(dòng)備份,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
ubuntu中使用docker下載華為opengauss數(shù)據(jù)庫超簡(jiǎn)單步驟
openGauss是關(guān)系型數(shù)據(jù)庫,采用客戶端/服務(wù)器,單進(jìn)程多線程架構(gòu),支持單機(jī)和一主多備部署方式,備機(jī)可讀,支持雙機(jī)高可用和讀擴(kuò)展,這篇文章主要給大家介紹了關(guān)于ubuntu中使用docker下載華為opengauss數(shù)據(jù)庫超的簡(jiǎn)單步驟,需要的朋友可以參考下2024-04-04
eXtremeDB 6.0正式發(fā)布:提高擴(kuò)展性和分布式查詢速度
這篇文章主要介紹了eXtremeDB 6.0正式發(fā)布:提高擴(kuò)展性和分布式查詢速度,本文詳細(xì)介紹了全新的eXtremeDB 6.0的一些特性,需要的朋友可以參考下2014-10-10
一款免費(fèi)開源的通用數(shù)據(jù)庫工具DBeaver
這篇文章主要介紹了一款免費(fèi)開源的通用數(shù)據(jù)庫工具DBeaver,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
替換一個(gè)字段的所有非數(shù)字字符為空的sql語句
2007-12-12
Win2003系統(tǒng)安裝SQL Server2000后1433端口未開放的解釋
這篇文章主要介紹了Win2003系統(tǒng)安裝SQL Server2000后1433端口未開放的解釋2007-02-02

