当存储过程返回多个结果集时应如何编写 dbml



有没有办法修改dbml,以便设计器.cs中的方法保持为IMultipleResults?

我有一个返回 2 组数据的存储过程。当我将过程拖放到 dbml、xml 和设计器时.cs由 dbml 生成

<Function Name="dbo.MultiResultsTest" Method="MultiResultsTest">
<Parameter Name="iCustomerID" Type="System.Int32" DbType="Int" />
<Parameter Name="iProductID" Type="System.Int32" DbType="Int" />
<ElementType Name="MultiResultsTestResult">
  <Column Name="CustomerID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="Name" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Phone" Type="System.String" DbType="VarChar(14)" CanBeNull="true" />
  <Column Name="Email" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Address" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
public ISingleResult<MultiResultsTestResult> MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return ((ISingleResult<MultiResultsTestResult>)(result.ReturnValue));
}

为了获得多个结果,我修改了设计器.cs如下所示:

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
[ResultType(typeof(Customer))]
[ResultType(typeof(Product))]
public IMultipleResults MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return (IMultipleResults)(result.ReturnValue);
}

这工作正常,但由于 dbml,当我向它添加更多内容并将方法保存在设计器中时.cs会恢复为 ISingleResult 一个。

经过一番搜索和尝试,我发现这种方式有效!

<Function Name="dbo.MultiResultsTest" Method="MultiResultsTest">
<Parameter Name="iCustomerID" Type="System.Int32" DbType="Int" />
<Parameter Name="iProductID" Type="System.Int32" DbType="Int" />
<ElementType Name="CustomerResult">
  <Column Name="CustomerID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="Name" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Phone" Type="System.String" DbType="VarChar(14)" CanBeNull="true" />
  <Column Name="Email" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Address" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>
<ElementType Name="ProductResult">
  <Column Name="ProductID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="ProductName" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Price" Type="System.Decimal" DbType="Decimal(18,2)" CanBeNull="true" />
  <Column Name="ModelNumber" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>

这会在设计器中生成以下方法.cs

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(CustomerResult))]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(ProductResult))]
public IMultipleResults MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return ((IMultipleResults)(result.ReturnValue));
}

最新更新