機房收費系統之組合查詢的傳參問題
最開始的時候,我用的是sql語句傳參的方式寫的,sql語句是這樣寫的(其中basicStudentInfo.FileName=cmbFieldName.SelectIndex,即實現了查詢欄位與下拉框選項的Index動態組合):
"select * from CardInfo,StudentInfowhere crdstudentno=stdstudentno and @FileName1" & _ basicStudnetInfo1.strOperator & "@value1 " & " " _ & basicStudnetInfo1.strRelation & " " & "@FileName2" & basicStudentInfo2.strOperator & "@value2" & " " _ & basicStudentInfo2.strRelation & " " & "@FileName3" & basicStudentInfo3.strOperator & "@value3"
其中basicStudentInfo是我定義的類,在我傳參的時候,我將@FileName傳入的是basicStudentInfo.FileName.tostring型別是char型。可是問題出來了,就是查不出來結果。然後我將@FileName引數去掉,直接採用的拼接字串,直接寫上basicStudentInfo.FileName.tostring,然後就出來結果了。剛開始我怎麼也想不出來結果,然後就先放著了。
"select *from CardInfo,StudentInfo where crdstudentno=stdstudentno and " & _ basicStudnetInfo1.fieldName.ToString & basicStudnetInfo1.strOperator & "@value1 " & " " _ & basicStudnetInfo1.strRelation & " " & basicStudentInfo2.fieldName.ToString & basicStudentInfo2.strOperator & "@value2" & " " _ & basicStudentInfo2.strRelation & " " & basicStudentInfo3.fieldName.ToString & basicStudentInfo3.strOperator & "@value3"
第二天看英語的時候,突然想到,這個表裡的欄位名寫在sql語句裡面應該是stdstudentno之類的詞語,可是如果像我這樣傳參的話,那麼查詢的欄位名就會變成'stdstudentno’,也就是'stdstudentno'='卡號'所以造成了我查不出來結果這件事情。
印象中聽過楊元說將表名作為引數傳入,那麼就會碰到和這個一樣的問題。然後向他請教了一下。
解決方法是這樣的,儲存過程中接收了這些引數後,然後在儲存過程中定義一個字元型別的臨時變數,在儲存過程中拼接這些字串,如果這樣的話,那麼四個組合查詢就可以都用這一個儲存過程了,那就是在傳參的時候,多傳一個表名進來就可以了。
上面就是我在組合查詢的過程中遇到的傳參問題。
總的來說,我的組合查詢中出去介面空值判斷的地方,我只在判斷relation除用到了選擇判斷語句,通過大話中的小例子,除去選擇判斷語句有利於擴充套件,所以在遇到選擇判斷語句時,我總是先想辦法避免,想不出來的時候再先寫上去。
我十分信奉一句話,每個人一生吃的苦的和是一定的。所以每想到一個簡單的方法,那麼就會遇到別的問題,例如在只有一個條件的時候,我必須去查三個條件的and關係。所以沒有哪種方法是簡單的,只有最適合的。