您是怎麼處理查詢的問題?我是又寫了一個控件來搞定的。
查詢控件
溫故而知新
能自己“跑”的表單控件,思路,雛形,源碼。vs2005版本
表單控件續(1)——應用接口來簡化和分散代碼
當初在寫表單控件的時候,突然想到,這個表單控件稍微修改一下不就是一個查詢控件了嗎?
那麼查詢控件需要做的什麼事情呢?
1、自己描繪控件,比如能夠自己添加文本框、下拉清單框這一類的控件。
2、可以自己擷取使用者輸入的資訊,根據查詢方式組合where 後面的 SQL語句 。
是不是和表單控件很像呢?
在表單控件的SaveData()裡面我們可以得到字段名稱和對應的使用者輸入的資訊,那麼我們就可以寫成這種方式。
for (int i = 0; i < info.Length; i++)
{
iCntl = this.FindControl("c_" + info[i].ColSysName) as IGetControlValue;
if (iCntl != null)
query = ColSysName + "like '%" + iCntl.GetControlValue() + "%'";
}
當然并不是所有的查詢都是隻用 like 就可以搞定的,這裡需要一個查詢方式的屬性,于是我們可以擴充一下ControlInfos 加一個屬性(FindKind),用來記錄查詢方式。
然後根據這個屬性,我們就可以來組合SQL語句了,準确的說是where 後面的SQL語句。目前的方法還是需要使用case 。
string str = iCntl.GetControlValue()
switch ( infos[i].FindKind)
case 1:
if (DoubleType.FromString(inputInfo[i]) != 0.0)
{
str2 = " [" + info[i].ColSysName + "]=" + str;
}
goto Label_0615;
case 2:
str2 = " [" + info[i].ColSysName + "]='" + str + "'";
case 3:
str2 = " [" + info[i].ColSysName + "] like '%" + str + "%'";
case 4:
str2 = " [" + info[i].ColSysName + "] like '" + str + "%'";
case 5:
str2 = " [" + info[i].ColSysName + "] like '%" + str + "'";
case 6:
str2 = " [" + info[i].ColSysName + "] like '" + str + "'";
case 11:
str2 = " [" + info[i].ColSysName + "]>'" + str + "'";
case 12:
str2 = " [" + info[i].ColSysName + "]<'" +str + "'";
case 13:
str2 = " [" + info[i].ColSysName + "]>='" +str + "'";
case 14:
str2 = " [" + info[i].ColSysName + "]<='" + str + "'";
case 0x15:
strArray3 = inputInfo[i].Split(new char[] { '|' });
if (strArray3[0].Length != 0)
goto Label_04B1;
str2 = "";
case 0x16:
strArray2 = inputInfo[i].Split(new char[] { '|' });
if (strArray2[0].Length != 0)
break;
case 0x17:
str3 = inputInfo[i].Replace("|", ",");
if (str3.Length != 0)
goto Label_0549;
case 0x18:
str4 = inputInfo[i].Replace(",", "','").Replace("|", "','");
if (str4.Length != 0)
goto Label_05CA;
case 30:
default:
不好意思,原來使用vb.net來寫的,這個是用Reflector反編譯的,不知道為什麼還給弄出來goto了。看來我寫的代碼不用混淆也可以了,我自己都看不懂了。還是發一個“原版”的吧。

Private Sub btn_click()Sub btn_click(ByVal Sender As Object, ByVal E As EventArgs)
'MyBase.Context.Response.Write("内部事件
")
Dim colInfo() As ColumnsInfo = _Col.SetColumnsInfos()
Dim inputInfo() As String = _Col.GetInputInfo
If inputInfo Is Nothing Then
System.Web.HttpContext.Current.Response.Write("_") '沒有輸入,或者輸入有誤!
Return
End If
'調用外部事件——查詢前
OnBtnClick(Sender, E)
'字段的查詢方式。1:= int;2:=string; 3:like %n%; 4:like n%; 5:like %n ;6:like n;11:> string;12:< string;13:>= string;14: <= string
Dim find As String = ""
Dim tmp As String = ""
Dim i As Int32
For i = 0 To colInfo.Length - 1
If inputInfo(i).Length > 0 And inputInfo(i) <> "-999999" And inputInfo(i) <> "_n_" Then
'輸入了資訊,組成查詢條件
Select Case colInfo(i).SearchKind
Case 1 '= int
If inputInfo(i) <> 0 Then
tmp = " [" & colInfo(i).ColSysName & "]=" & inputInfo(i)
End If
Case 2 '= string
tmp = " [" & colInfo(i).ColSysName & "]='" & inputInfo(i) & "'"
Case 3 'like %n%
tmp = " [" & colInfo(i).ColSysName & "] like '%" & inputInfo(i) & "%'"
Case 4 'like n%
tmp = " [" & colInfo(i).ColSysName & "] like '" & inputInfo(i) & "%'"
Case 5 'like %n
tmp = " [" & colInfo(i).ColSysName & "] like '%" & inputInfo(i) & "'"
Case 6 'like n
tmp = " [" & colInfo(i).ColSysName & "] like '" & inputInfo(i) & "'"
Case 11 '> string
tmp = " [" & colInfo(i).ColSysName & "]>'" & inputInfo(i) & "'"
Case 12 '< string
tmp = " [" & colInfo(i).ColSysName & "]<'" & inputInfo(i) & "'"
Case 13 '>= string
tmp = " [" & colInfo(i).ColSysName & "]>='" & inputInfo(i) & "'"
Case 14 '<= string
tmp = " [" & colInfo(i).ColSysName & "]<='" & inputInfo(i) & "'"
Case 22 'between int
Dim aa() As String = inputInfo(i).Split("|")
If aa(0).Length = 0 Then
tmp = ""
Else
If aa(1).Length = 0 Then
aa(1) = aa(0)
End If
tmp = " [" & colInfo(i).ColSysName & "] between " & aa(0) & " and " & aa(1)
Case 21 'between string
tmp = " [" & colInfo(i).ColSysName & "] between '" & aa(0) & "' and '" & aa(1) & "'"
Case 23 'col in () 多選查詢 數字方式
Dim a23 As String = inputInfo(i).Replace("|", ",")
'System.Web.HttpContext.Current.Response.Write(a23)
If a23.Length = 0 Then
tmp = " [" & colInfo(i).ColSysName & "] in (" & a23 & ")"
Case 24 'col in () 多選查詢 字元串方式
Dim a23 As String = inputInfo(i).Replace(",", "','")
a23 = a23.Replace("|", "','")
tmp = " [" & colInfo(i).ColSysName & "] in ('" & a23 & "')"
Case 30 '不生成查詢條件
tmp = ""
End Select
'加到查詢條件裡面
If tmp.Length > 0 Then
If find.Length > 0 Then
find &= " and " & tmp
Else
find = tmp
End If
End If
End If
tmp = ""
Next
If _OutSearch.Length > 0 Then
'有外部的查詢條件,加到 find 裡面
If find.Length > 0 Then
find = find & " and " & _OutSearch
Else
find = _OutSearch
'System.Web.HttpContext.Current.Response.Write(find)
If Not _PageTurn Is Nothing Then
If _PageTurn.SetQuery.Length = 0 Then
_PageTurn.SqlQuery = find
If find.Length = 0 Then
_PageTurn.SqlQuery = _PageTurn.SetQuery
Else
_PageTurn.SqlQuery = _PageTurn.SetQuery & " and " & find
_Search = find
_PageTurn.SqlQuery = find
_PageTurn.CreateQuery()
_PageTurn.BindFirstPage()
End Sub
#End Region
當然這裡有一個适用範圍和習慣的問題。我是習慣使用DataTable來裝載資料,而填充 DataTable 需要SQL語句,那麼我隻需要得到 where 後面的部分,查詢的功能就可以實作了,是以對于我來說,查詢控件能夠輸出像 "myName like '%jyk%'" 這樣的字元串就已經夠用了。
還記得分頁控件嗎?分頁控件有一個屬性:myPage.SqlQuery = "";這個屬性就是用來給分頁控件設定查詢條件的,正好可以查詢控件對應上,這兩個控件一配合起來,查詢、分頁就變得非常的簡單了。
對其他的查詢方式的支援的考慮
我沒有用過使用實體類來顯示資料的方式,是以也不知道對于實體類來說,查詢是怎麼做的,不過這個查詢控件至少可以提供字段名和對應的值,應該是可以用得上的吧。
可能有些人喜歡使用存儲過程來傳回記錄,查詢條件也會寫在存儲過程裡面,那麼在調用存儲過程的時候需要傳遞存儲過程的參數,這種情況,查詢控件也可以幫上點忙吧。
抽象
這樣出現了一個問題,由于兩個控件比較象,但是總不能等表單控件寫好了,然後複制粘貼,再改一改,查詢控件就誕生了吧。我們是不是應該對于相同的地方進行“抽象”呢,把相同的代碼放在基類裡面。可能你會說,就兩種情況,有必要抽象嗎,還弄一個基類出來是不是多此一舉呢?我的回答是:很有必要。除非這兩個控件件寫完了之後就再也不需要修改了。
隻是單獨寫很好寫,不用顧忌其他,但是要考慮到其他的用法的話,那就要十分小心了。