多條件檢索SQL語句的拼接

2022-03-14 07:11:20 字數 935 閱讀 3223

需求分析 :

在使用多條件查詢的時候,比如這樣的乙個圖書查詢頁面:

如果使用sql語句: select * from book where bookname='name' and author='author' and address='address『

但是,在不知道哪一欄會被輸入進去,where和and 的使用,有點不知所錯,當然,也可以把所有可能的情況所使用的sql語句乙個乙個的邏輯判斷,

顯然,這樣子是很不合適的,這時候,就需要對sql語句的拼接。

string sql = "

select * from book";

stringbuilder sb = new

stringbuilder();

list

wheres = new list();

list

parma = new list();

if (textbox1.text.length > 0

)

if (textbox2.text.length > 0

)

if (textbox3.text.length > 0

)

//有乙個條件,就新增where,然後多條件就在中間加and

if (wheres.count > 0

)

結:使用到list集合,以及字串處理的插入 。需要注意的是,list集合中的sql條件語句 要多加乙個或者兩個空格,避免把 where或者and插入的時候,兩個字串粘在一起,導致sql語句發生錯誤

多條件儲存過程 (條件拼接SQL語句)

alter proc sp pagedbyconditions countrycode nvarchar 20 國家編號 cid int,城市的id pageindex int 1,當前要顯示的頁碼 pagesize int 3,每頁要顯示的資料條數 頁大小 totalpages intout 總頁...

多條件查詢時,拼接原生sql語句的方法

完善 author 張齊 說明 根據前台用輸入的查詢條件,得到乙個原生查詢語句 param searchcondition 引數陣列 return sqlquery 返回的原生sql語句 public sqlquery getsqlquery string searchcondition if se...

phpsql拼接 PHP拼接多條件SQL語句

判斷是否新增where語句 param sql param has where return string sql has where and where return sql 檢索資料 param params return activedataprovider public function s...