2013-05-29 57 views
0

我通常聲明我DAL類覆蓋的搜索方法只要有可能,如下的例子:在模式DAL中實現搜索的最佳方式是什麼?

public class clsAsd { 
    private AdConn oConn; 

    public clsAsd(ref AdConn Connection) { 
     oConn = Connection; 
    } 

    private string sqlSearch(string DocType, string Status, string Aaa) { 
     return 
      " AND x.tp_doc = '" + DocType + "'\r\n" + 
      " AND x.co_status IN (" + Status + ")\r\n" + 
      " AND x.aaa = '" + Aaa + "'\r\n"; 
    } 
    public List<clsQwe> search(string DocType, string Status, string Aaa, bool IncDel) { 
     return search(sqlSearch(DocType, Status, Aaa), IncDel); 
    } 

    private string sqlSearch(string DocType, string Status, string Aaa, string Bbb) { 
     string sSQL = sqlSearch(DocType, Status, Aaa) + (Bbb != "" ? " AND x.bbb = '" + Bbb + "'\r\n" : ""); 
     return sSQL; 
    } 
    public List<clsQwe> search(string DocType, string Status, string Aaa, string Bbb, bool IncDel) { 
     return search(sqlSearch(DocType, Status, Aaa, Bbb), IncDel); 
    } 

    private List<clsQwe> search(string Where, bool IncDel) { 
     string sSQL; 

     sSQL = "SELECT\r\n"; 
     sSQL += " b.aaa, c.bbb, x.*\r\n"; 
     sSQL += "FROM asd x, qwe b, zxc c\r\n"; 
     sSQL += "WHERE x.www = b.www\r\n"; 
     sSQL += " AND x.zzz = c.zzz\r\n"; 
     sSQL += Where; 

     if (!IncDel) sSQL += " AND x.del IS NULL\r\n"; 

     sSQL += "ORDER BY x.www"; 

     // Connection + Run SQL 

     // List to get results 
     List<clsQwe> lstRet = new List<clsQwe>(); 

     // Row to add in list 
     clsQwe oX; 

     while (oReader.Read()) { 
      oX = new clsQwe(); 
      // Add all data 
      lstRet.Add(oX); 
     } 

     // Return data 
     return lstRet; 
    } 
} 

我的問題是:這是一個很好的做法?

我應該爲每個不同的搜索使用一種方法? 示例:

public class clsAsd { 
    private AdConn oConn; 
    public clsAsd(ref AdConn Connection) { oConn = Connection; } 

    public List<clsQwe> search1(string DocType, string Status, string Aaa, bool IncDel) { } 
    public List<clsQwe> search2(string DocType, string Status, string Aaa, string Bbb, bool IncDel) { } 
    public List<clsQwe> search3(string DocType, string Status, string Aaa, string Bbb, string Ccc, bool IncDel) { } 

    private List<clsQwe> search(string Where, bool IncDel) { } 
} 

我該如何改進這個課程?

我正在考慮只實現一個接收類(clsQwe)作爲參數的搜索方法。我檢查這個類的每個屬性,並根據所填寫的屬性創建where子句。

這種方法很有趣?

謝謝。

+0

不,它不是。您打開[sql-injection](http://msdn.microsoft.com/en-us/library/ms161953(v = sql.105).aspx),使用參數化查詢。 –

+0

我知道這一點,而且我正在使用Oracle命令進行實際實施。我的問題是如何最好地實施我的課程搜索?謝謝。 –

回答

0

爲了改善你的類,直接使用數據庫,你可以使用ORM

相關問題