![](/img/trans.png)
[英]How can I pass the Hidden Field value in SqlCommand in C# asp.net Three Tier Architecture?
[英]How to return data from the database using 3 tier architecture structure using ASP.NET C#
假設我有一個用C#編寫的3層ASP.NET應用程序。 您應該如何正確利用DAL,BLL和PL?
例如,假設我有一個存儲過程,該過程要求返回客戶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中 ,我將獲得該返回的表並填充數據集?
我試圖填充沒有我的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;
}
}
但是我得到這些錯誤:
另外:如何綁定數據集,以便可以將數據返回到我的文本框?
數據集具有一個表集合-您只需要從數據集中返回第一個表:
var dataSet = new DataSet();
adapt.Fill(dataSet, "table");
return dataSet.Tables["table"];
另外,不要這樣做,因為它會破壞stacktrace :
catch (Exception ex)
{
throw (ex);
}
如果您不打算進行任何異常處理,請完全放棄try / catch。 如果要進行處理然后重新引發,則可以throw
,也可以包裹並拋出 (例如, throw new SomeException("Wrapped", ex);
)
最后,請注意,DAL中的許多對象都是IDisposable
-DataReaders,SqlConnection和SqlCommand都應該被處置-我建議將調用包裝在using
范圍內。
我在一堂課中實現了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();
}
請讓我知道,如果你有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.