如何使用 ASP.NET C# 使用 3 层架构结构 return 来自数据库的数据
How to return data from the database using 3 tier architecture structure using ASP.NET C#
假设我有一个用 C# 编写的 3 层 ASP.NET 应用程序。你应该如何正确使用 DAL、BLL 和 PL?
例如,假设我有一个存储过程,它需要在 return 产生结果之前传入客户 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 中,我会得到 returned table 并填充数据集?
我试图在没有调用 DataTable
的情况下填充数据集,"table"
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;
}
}
但是我得到这些错误:
另外:如何绑定数据集以便我可以 return 将数据添加到我的文本框?
DataSet 有一个表集合 - 您只需要 return 来自 DataSet 的第一个 Table:
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
- DataReaders、SqlConnection 和 SqlCommand 都应该被处理掉 - 我建议将调用包装在 using
范围内。
我已经将 BL(业务层)和 DAL(数据访问层)合二为一 class。
例如,我的数据库中有一个 table 调用 "Clarity_Master"。所以,我在 visual studio 名称中添加了一个 class 作为 "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 classes。
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();
}
如果您有任何问题,请告诉我。
假设我有一个用 C# 编写的 3 层 ASP.NET 应用程序。你应该如何正确使用 DAL、BLL 和 PL?
例如,假设我有一个存储过程,它需要在 return 产生结果之前传入客户 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 中,我会得到 returned table 并填充数据集?
我试图在没有调用 DataTable
的情况下填充数据集,"table"
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;
}
}
但是我得到这些错误:
另外:如何绑定数据集以便我可以 return 将数据添加到我的文本框?
DataSet 有一个表集合 - 您只需要 return 来自 DataSet 的第一个 Table:
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
- DataReaders、SqlConnection 和 SqlCommand 都应该被处理掉 - 我建议将调用包装在 using
范围内。
我已经将 BL(业务层)和 DAL(数据访问层)合二为一 class。
例如,我的数据库中有一个 table 调用 "Clarity_Master"。所以,我在 visual studio 名称中添加了一个 class 作为 "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 classes。
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();
}
如果您有任何问题,请告诉我。