【问题标题】:How to return data from the database using 3 tier architecture structure using ASP.NET C#如何使用 ASP.NET C# 使用 3 层架构结构从数据库返回数据
【发布时间】:2015-06-09 22:15:57
【问题描述】:

假设我有一个用 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 的情况下填充数据集,称为“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;
    }
}

但是我得到了这些错误:

另外:如何绑定数据集以便我可以将数据返回到我的文本框?

【问题讨论】:

    标签: c# asp.net datatable 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。如果您要进行处理然后再加注,则只需throwwrap and throw(例如throw new SomeException("Wrapped", ex);

    最后,请注意 DAL 中的许多对象是 IDisposable - DataReaders、SqlConnection 和 SqlCommand 都应该被释放 - 我建议将调用包装在 using 范围内。

    【讨论】:

    • Re:如何将 DataTable 绑定到 Asp.Net UI - 你没有提到是 WebForms 还是 MVC,但在 WebForms 中 here's a basic example
    • 我认为 DAL、BLL、PL 的全部意义在于,您无需从 PL... 分离后面的代码中触发 SQL...!
    • 是的,同意我们不希望 Sql 泄漏出 Data 层。通常,人们已经远离像DataTable(和类型化数据集)这样的弱模式,而转向从数据/存储库返回实体 POCO,例如通过 ORM 之类的实体框架和 Linq2Sql。此外,您还可以查看 Onion layering your architecture 而不是“N tier” - 例如通常很难为 Fetch 和 Query 活动证明“业务层”的合理性。 Onion 也是通过依赖注入解耦层的自然副作用。
    【解决方案2】:

    我在一个类中实现了 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();
        }
    

    如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 2016-11-08
      相关资源
      最近更新 更多