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

