假设我有一个用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
范围中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句