2015-04-05 26 views
1

假設我有一個用C#編寫的三層ASP.NET應用程序。你應該如何正確使用DAL,BLL和PL?如何使用3層架構結構使用ASP.NET C從數據庫返回數據#

例如,假設我有一個存儲過程,它在返回結果之前需要傳入一個客戶ID參數。在我的數據訪問層,我可以有以下幾種:

public DataTable GetCustomerInfo(collection b) 
{ 
    DataTable table; 

    try 
    { 
     string returnValue = string.Empty; 
     DB = Connect(); 
     DBCommand = connection.Procedure("sp_getCust"); 
     DB.AddInParameter(DBCommand, "@CustomerID", DbType.String, b.CustomerID); 

     DbDataReader reader = DBCommand.ExecuteReader(); 
     table = new DataTable(); 
     table.Load(reader); 
     return table; 
    } 
    catch (Exception ex) 
    { 
     throw (ex); 
    } 
} 

然後在我的BLL,我會再拿到返回的表和填充DataSet?

我試圖填充DataSet沒有我DataTable稱爲 「表」

public static DataTable returnCustomer(collection b) 
{ 
    try 
    { 
      SqlDataAdapter adapt = new SqlDataAdapter(); 
      DataSet table = new DataSet(); 

      adapt.Fill(table, "table"); 
      return table; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

但是我得到這些錯誤:

Error returning datatable to dataset.

另外:如何綁定數據集,使我可以將數據返回到我的文本框?

回答

1

DataSet有一個表集合 - 你需要從DataSet只返回第一個表:

var dataSet = new DataSet(); 
adapt.Fill(dataSet, "table"); 
return dataSet.Tables["table"]; 

而且,不這樣做,因爲它destroys the stacktrace

catch (Exception ex) 
{ 
    throw (ex); 
} 

如果您不會做任何異常處理,然後完全放棄try/catch。如果你打算做處理,然後重新加註,然後要麼只是throw,或wrap and throw(如throw new SomeException("Wrapped", ex);

最後請注意,許多在你的DAL對象都是IDisposable - 的DataReader,SqlConnection的和SqlCommand的都應該處置 - 我會建議在using範圍內打電話。

+0

Re:如何將一個DataTable綁定到一個Asp.Net UI - 你沒有提到WebForms或MVC,但[這裏是一個基本的例子](http://stackoverflow.com/a/29428473/314291) WebForms – StuartLC 2015-04-05 09:11:45

+0

** webforms ** .... – PriceCheaperton 2015-04-05 09:55:32

+0

我認爲DAL,BLL,PL的全部意義在於,您無需從PL背後的代碼中激發SQL ...分離......! – PriceCheaperton 2015-04-05 09:56:16

0

我在一個類中實現了BL(業務層)和DAL(數據訪問層)。

例如,我在我的數據庫中有一個表叫「Clarity_Master」。所以,我有添加在Visual Studio名一類爲 「Clarity_BLL.cs」

Clarity_BLL.cs

namespace DAL 
{ 
    public class Clarity_BLL 
    { 
    public int PURITY_ID { get; set; } 
    public string PURITY_NAME { get; set; } 
    public string PURITY_CODE { get; set; } 
    public int DISPLAY_ORDER { get; set; } 
    public bool IDELETE { get; set; } 

    public DataTable GET_CLARITYBYNAME() 
    { 
     ExceptionManager exManager; 
     exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>(); 
     DataTable dt = null; 
     try 
     { 
      exManager.Process(() => 
      { 
       Database sqlDatabase = DBConnection.Connect(); 
       DataSet ds = sqlDatabase.ExecuteDataSet("StoreProcedureName",PURITY_NAME_Para1, IDELETE_Para2); 
       dt = ds.Tables[0]; 
      }, "Policy"); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     return dt; 
    } 
    } 
} 

而在我的PL使用BL & DAL類,如下面的(表示層)。

Clarity_MST.aspx.cs

public void bindgrid() 
    { 
     Clarity_BLL obj_CLARITY_BLL = new Clarity_BLL(); 
     obj_CLARITY_BLL.PURITY_ID = 0; 
     obj_CLARITY_BLL.IDELETE = true; 
     grdClarity.DataSource = obj_CLARITY_BLL.GET_CLARITYBYNAME(); 
     grdClarity.DataBind(); 
    } 

請讓我知道如果您有任何問題。