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

时间:2017-05-24 05:41:46

标签: c# linq-to-sql dbml

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

我有一个存储过程,返回2组数据。 我拖的时候将过程删除到dbml

生成的dbml,xml和designer.cs
<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));
}

为了获得多个结果,我将designer.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,当我向它添加更多内容并将该方法保存在designer.cs中时,将恢复为ISingleResult。

1 个答案:

答案 0 :(得分:0)

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

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

这在designer.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));
}