繁体   English   中英

如何使用ASP.NET C#使用3层体系结构从数据库返回数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM