简体   繁体   English

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

[英]How to return data from the database using 3 tier architecture structure using ASP.NET C#

Let's say I have a 3-tier ASP.NET application written in C#. 假设我有一个用C#编写的3层ASP.NET应用程序。 How are you supposed to utilize the DAL, BLL and PL correctly? 您应该如何正确利用DAL,BLL和PL?

For example let's say I have a stored procedure which requires a parameter of customer ID to be passed in before it returns results. 例如,假设我有一个存储过程,该过程要求返回客户ID的参数,然后返回结果。 In my data access layer I could have the following: 在我的数据访问层中,我可以有以下内容:

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);
    }
}

Then in my BLL , I would then get that returned table and populate a dataset? 然后在BLL中 ,我将获得该返回的表并填充数据集?

I have tried to populate a dataset without my DataTable called, "table" 我试图填充没有我的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;
    }
}

However I get these error: 但是我得到这些错误:

将数据表返回到数据集时出错。

Also: how to bind the dataset so that I can return the data to my textboxes? 另外:如何绑定数据集,以便可以将数据返回到我的文本框?

DataSet has a tables collection - you need to return just the first Table from the DataSet: 数据集具有一个表集合-您只需要从数据集中返回第一个表:

var dataSet = new DataSet();
adapt.Fill(dataSet, "table");
return dataSet.Tables["table"];

Also, don't do this, as it destroys the stacktrace : 另外,不要这样做,因为它会破坏stacktrace

catch (Exception ex)
{
     throw (ex);
}

If you aren't going to do any exception handling, then drop the try / catch entirely. 如果您不打算进行任何异常处理,请完全放弃try / catch。 If you are going to do handling and then re-raise, then either just throw , or wrap and throw (eg throw new SomeException("Wrapped", ex); ) 如果要进行处理然后重新引发,则可以throw ,也可以包裹并抛出 (例如, throw new SomeException("Wrapped", ex);

Finally, note that many of the objects in your DAL are IDisposable - DataReaders, SqlConnection and SqlCommand should all be disposed - I would recommend wrapping the call in a using scope. 最后,请注意,DAL中的许多对象都是IDisposable -DataReaders,SqlConnection和SqlCommand都应该被处置-我建议将调用包装在using范围内。

I have implementing BL (Business Layer) and DAL (Data Access Layer) in one class. 我在一堂课中实现了BL(业务层)和DAL(数据访问层)。

For example, I have one table call "Clarity_Master" in my database. 例如,我的数据库中有一个表调用“ Clarity_Master”。 So, I have add one class in visual studio name as "Clarity_BLL.cs" 因此,我在Visual Studio名称中添加了一个类作为“ Clarity_BLL.cs”

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;
    }
  }
 }

And use this BL & DAL classes like below in my PL (Presentation Layer). 并在我的PL(演示层)中使用下面的BL和DAL类。

Clarity_MST.aspx.cs 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();
    }

Please let me know if you have any questions. 请让我知道,如果你有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#asp.net三层体系结构的SqlCommand中传递“隐藏字段”值? - How can I pass the Hidden Field value in SqlCommand in C# asp.net Three Tier Architecture? 如何使用c#将asp.net中数据库中的数据赋值到二维数组? - How to assing values to 2 dimensional array with data from database in asp.net using c#? 使用ASP.NET 5的N层架构(MVC 6) - N-Tier Architecture using ASP.NET 5 ( MVC 6 ) 如何使用 c# asp.net 将数据库中的数据插入表中? - How to insert data from database into a table using c# asp.net? 如何使用C#显示asp.net中数据库中的数据 - how to show data from database in asp.net using c# 如何使用asp.net/c#从数据库连接数据并将其发送到字符串? - How to concat data from database and send it to a string using asp.net/c#? 试图使用asp.net和c#将数据插入数据库 - trying to insert data into a database using asp.net and c# 在ASP.NET中使用C#将数据添加到数据库 - Adding data to a database using C# in asp.net 使用asp.net C#将数据插入数据库 - inserting data into database using asp.net c# 如何使用asp.net C#中的绑定功能在GridView中编辑删除数据行,其中数据来自数据库 - how to edit delete data row in gridview where data come from database using binding function in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM