【问题标题】:How to return multi-table join value from BLL如何从 BLL 返回多表连接值
【发布时间】:2011-03-20 21:53:31
【问题描述】:

这个问题是关于我使用 DAL-BLL 架构为我的期末学校项目创建的 ASP.NET 网络服务。

我有一个存储过程,它是一个带有 2 个表的内连接的选择查询。因此存储过程返回多表值。我的 DAL tableAdapter 方法之一访问此存储过程。如何检索 BLL 中的返回值?我是否必须创建一个类似于应该由存储过程返回的类结构?还是有直接的方法来实现相同的目标?非常感谢帮助。如果有人需要代码小程序以更好地理解,请告诉我。谢谢

以下是更多信息: 我在 DAL 中使用 SQL 数据集 (.xsd)。所以我有一个名为“Insurance”的数据表,它有一个 tableAdapter。适配器中的一个查询引用了一个存储过程,该过程具有一个内部连接。所以我的SP看起来像:

ALTER PROCEDURE dbo.GetInsurancesPaged
    (
        @startRowIndex int,
        @maximumRows int,
        @patientID int
    )
AS
    select * from
    (
    SELECT Insurance.insuranceID, Insurance.memberID, Insurance.groupID, Insurance.accountType, Insurance.comments, Insurance.patient, Insurance.company, InsuranceCompany.companyID, InsuranceCompany.companyName, InsuranceCompany.address, InsuranceCompany.phone, InsuranceCompany.fax, ROW_NUMBER() over (order by Insurance.dateModified DESC) as ROWRANK
FROM Insurance INNER JOIN InsuranceCompany ON Insurance.company = InsuranceCompany.companyID
WHERE Insurance.patient = @patientID
    )
    AS DataWithRowNumbers
WHERE ROWRANK > @startRowIndex AND ROWRANK <= (@startRowIndex + @maximumRows)

所以这个 SP 返回一个数据表,它将是内部连接中 2 个表的组合。如果我错了,请纠正我。

现在在我的 BLL 中,我有:

[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
    public mySys.InsuranceDataTable GetInsurancesPaged(int startRowIndex, int maximumRows, int patientID)
    {
       return insAdapter.GetInsurancesPaged(startRowIndex, maximumRows, patientID);
    }

insAdapter 是 insuranceTableAdapter 的一个实例

这会在执行时出错。我可以成功执行 SP,所以我认为问题只是我试图从 BLL 返回错误的数据表。 请帮我解决这个问题。

【问题讨论】:

  • 是的 - 我们需要一些代码。需要查看 SQL Result Set 的样子,以及您使用的 DAL 技术(LINQ-SQL、经典 ADO.NET 等)
  • 感谢您的浏览。我在我的问题中提供了更多信息。请告诉我如何解决它。

标签: asp.net data-access-layer bll multi-table


【解决方案1】:

如果使用 ADO .Net 数据集。该向导将明确地为此创建一个表。现在从数据访问层,执行以下步骤

1. Create a object of dataset. (DLL)

Private YourCustomeDataSetDatatable  DataAccess()
{
 YourCustomDataSet ds = new YourCustomDataSet();  // also called strongly typed dataset
YourCustomeDataSetDatatable dt = ds.YourCustomeDataSetDatatable ()
YourCustomeDataSetTableAdapter ta = new ds.YourCustomeDataSetTableAdapter (); // table adapter that will be invoked 
ta.Fill(dt); // or if you have set to return only you can also use GetData()
}

2. Now in business layer

Private YourCustomeDataSetDatatable  DataAccess()
{
// create a object of DLL. 
MyDAL myDal = new MyDAL ();
return myDal.DataAccess();
}
  1. 按照 BLL 的创建对象并调用该方法,在您的 UI 页面上捕捉到这一点。在 BLL 中,您还可以执行各种操作来降低 ui 中的代码并使其免受各种操作的影响。

【讨论】:

  • 感谢 Amit,但我无法将您的解决方案与我的情况联系起来。我在我的问题中添加了一些代码,请让我知道您的解决方案如何适用。
【解决方案2】:

找到了解决办法:) 终于搞定了。

我使用数据集设计器创建了一个新的表适配器,并将 SP 称为那里的查询之一。如此创建的数据表包含所有字段(来自 Insurance 和 InsuranceCompany)。现在,ASP.NET 可以检测到返回类型是新创建的数据表。 像魅力一样工作。

如果有更好的方法解决这个问题,请评论。

感谢大家的宝贵时间。

【讨论】:

    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多