多條件查詢的程序
更新時間:2009年05月27日 01:45:38 作者:
在一個網(wǎng)站中,常常會使用到查詢功能。假設一個企業(yè)內部網(wǎng)中,用戶信息里通常會涉及到工號、姓名、性別、學歷、職業(yè)、職稱、身份證號碼、手機號碼、座機號碼、傳真號碼、郵政編號、通訊地址等信息。
而在對用戶進行查詢時,也可能會使用到多種條件的查詢方式,如通過工號查詢、通過姓名查詢、通過性別查詢、通過學歷查詢等。也有可能會通過多種條件的組合查詢,如查學歷是大專的女員工等。
對于這種查詢情況,通常的作法是讓用戶輸入查詢條件,再進行SQL語句組合來進行查詢。如讓用戶輸入工號、姓名等,單擊提交按鈕之后,在后臺獲得這些信息,如以下代碼所示:
//設置查詢語句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用戶名不為空則添加查詢條件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性別不為空則添加查詢條件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在創(chuàng)建完SQL語句之后,執(zhí)行該語句獲得查詢結果。
這種是使用得最多并且是最不安全的方法,因為這是最容易讓別人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語句組合來進行查詢。如讓用戶輸入工號、姓名等,單擊提交按鈕之后,在后臺獲得這些信息,如以下代碼所示:
復制代碼 代碼如下:
//設置查詢語句
string strSql = "SELECT * FROM [user] where UserState=1 ";
//如果用戶名不為空則添加查詢條件
if (UserName!="")
{
strSql += "and (UserName'= "+UserName+"') ";
}
//如果性別不為空則添加查詢條件
if (Sex!="")
{
strSql += "and (Sex'= "+Sex+"') ";
}
在創(chuàng)建完SQL語句之后,執(zhí)行該語句獲得查詢結果。
這種是使用得最多并且是最不安全的方法,因為這是最容易讓別人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
相關文章
jdbc 數(shù)據(jù)庫的連接(sqlserver oracle)
sql Server 和oracle 數(shù)據(jù)庫的連接,供大家參考!2009-08-08使用SQL Mail收發(fā)和自動處理郵件中的擴展存儲過程簡介
使用SQL Mail收發(fā)和自動處理郵件中的擴展存儲過程簡介...2006-12-12如何判斷a、b、c三個字段同時為0則不顯示這條數(shù)據(jù)
有時候我們需要判斷當a、b、c三個字段同時為0則不顯示,下面這個方法不錯,需要的朋友可以參考下2013-08-08Navicat 導出導入數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了Navicat 導出導入數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11數(shù)據(jù)庫觸發(fā)器(Trigger)的一點使用心得
最近了解了一下數(shù)據(jù)庫觸發(fā)器,并做一點實際的應用,在翻看其概念的時候,還是本著從理解的角度來學習的,但是,到了實際的應用場景中,還是有一些特別注意的地方的,下面是自己在應用中的幾點體會2009-07-07