2008-11-01 70 views
4

我有一個名爲call的數據庫表,其中列call_time,location,emergency_type,並且有三種類型的緊急情況:醫務人員,警察和消防員。在Windows窗體中,我創建了CheckBoxes的「護理人員」,「警察」,「消防員」,我想檢索滿足用戶選擇的所有表格列。檢索符合選定條件的數據的最佳做法

我創建了一個功能:

public static DataTable GetHistory(DateTime from, DateTime to, bool paramedics, bool police, bool firefighters) 
    { 
     string select = 
      "SELECT call_time, location, emergency_type where call_time between @from AND @to AND"; 
     if(paramedics) 
     { 
      select += " emergency_type = 'paramedics' "; 
     } 
     if(paramedics && police) 
     { 
      select +=" emergency_type = 'paramedics' OR emergency_type = 'police'; 
     } 
     ... 

    } 

此代碼然而看起來非常髒,因爲如果有30個各種緊急會有30!在編寫所有if語句之前,我會變老。

如果您共享您檢索符合所選搜索條件,如果有,你可以chosse很多選擇數據的做法我將不勝感激。

謝謝!

+0

順便說一句,如果語句運行了一個又一個,所以如果醫護人員和警察都是真的,你wouold用字符串」 ...... emergency_type最終=‘救護’emergency_type =‘救護’OR emergency_type =‘警察’ 「 – Ady 2008-11-01 08:58:17

+0

這只是一個方法示例,而不是工作版本。 – 2008-11-01 20:02:22

回答

5

那麼如果你必須使用emergency_type作爲一個字符串,然後強似在布爾變量,你可以在包含緊急類型的文本表示列表發送。例如,要調整上面的代碼,你可以在方法簽名更改爲

public static DataTable GetHistory(DateTime from, DateTime to, List<string> types) 
{ 
.. 
} 

,然後通過在一個看起來像這些(例如)

List<string> types = 
    new List<string> { "paramedics" }; 

or 

List<string> types = 
    new List<string> { "paramedics", "police" }; 

列表然後你可以調整你的查詢中使用where子句中的SQL IN語句。接下來字符串列表轉換成一個逗號分隔的字符串喜歡

string values = "'paramedics', 'police'" 

一個簡單的方法來創建數值變量是使用

string values = string.Empty; 
      types.ForEach(s => 
      { 
       if (!string.IsNullOrEmpty(values)) 
        values += ","; 
       values += string.Format("'{0}'", s); 

      }); 

順便說一句,你可以使用參數化的命令,以避免SQL注入。一旦你有字符串,你可以簡單地做

string select = 
"SELECT call_time, location, emergency_type where call_time between @from AND @to AND emergency_type IN " + values 
0

這是一個骯髒的做法。

string select = "SELECT call_time, location, emergency_type where call_time between @from AND @to AND (1=0"; 

if(paramedics) { select += " OR emergency_type = 'paramedics' "; } 
if(police)  { select += " OR emergency_type = 'police'"; } 
if(xyz)  { select += " OR emergency_type = 'xyz'"; } 

select += ")"; 
0

應該避免字符串連接,因爲它可以導致一些討厭的漏洞。 如果您正在尋找有關程序化訪問的最佳實踐,那麼此處的最佳做法是使用參數化查詢。

如果您想要便宜,請使in子句採用一個參數,並將該字符串連接在一起,並將其作爲in子句的參數值傳遞給checkbox。它應該是這樣的:

where ... and emergency_type in (?) 

的另一種方式做到這一點是計算所選中的複選框的數量,並建立參數列表的條款,所以它看起來更像是這樣的:

where ... and emergency_type in(?,?...) -- as many params as there are checked checkboxes. 

其中任何一個都可以。 有了這些類型的查詢,我已經走了這麼遠,建立自己的SQL構造方法,我把參數的內部計數,他們的數據類型和動態生成SQL語句,然後執行與良好的已知列表準備參數。

你可能會看看學習Linq。

0

構建用戶的比較值列表(@EmergencyList),並使用包含運算符的參數化查詢使用SQL。

SELECT call_time, 
     location, 
     emergency_type 
where call_time between @from AND @to 
    AND CONTAINS(Emegency_Type, @EmergencyList) 
相關問題