存储过程使用设计器返回多个结果集Sql To Linq

时间:2010-06-23 07:19:52

标签: sql linq-to-sql stored-procedures multiple-results

我想从使用sql到linq的storedProc中获取多个结果集。我无法从设计器生成它,所以我在designer.cs文件中编写了下面的代码。但每当我向设计器添加内容时,它会使用.dbml文件中的标记刷新设计器,因此每次添加内容时都会删除下面的代码。我每次都要复制它。如果我能为此获得相应的dbml标记,那就太棒了。

[Function(Name = "dbo.GetAllModulesAndOptions")]
[ResultType(typeof(Module))]
[ResultType(typeof(ModuleOption))]
public IMultipleResults GetAllModules()
{
  IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
  return ((IMultipleResults)(result.ReturnValue));
}

我已经将Module和ModuleOption定义为表格。 现在,当我在.dbml文件中添加以下标记时,它会抱怨 DBML1114: The Name attribute 'Module' of the Type element is already used by another type.

  <Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
    <ElementType Name="Module">
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
      <Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
      <Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
    <ElementType Name="ModuleOption">
      <Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
      <Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
  </Function>

我正在使用Visual Studio 2008 SP1

2 个答案:

答案 0 :(得分:0)

将方法添加到数据上下文的分部类中。

您可以通过在dbml文件旁边添加一个与数据上下文同名的文件来实现此目的,并使用类声明:

public partial class YourDataContext
{
    [Function(Name = "dbo.GetAllModulesAndOptions")]
    [ResultType(typeof(Module))]
    [ResultType(typeof(ModuleOption))]
    public IMultipleResults GetAllModules()
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo (MethodInfo.GetCurrentMethod())));
        return ((IMultipleResults)(result.ReturnValue));
    }   
}

答案 1 :(得分:0)

我在回答我自己的回答。

不能将已定义的类型用于存储过程的结果集。所以我不得不将ElementType的名称更改为ModuleResult和ModuleOptionResult。

  <Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
    <ElementType Name="ModuleResult">
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
      <Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
      <Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
    <ElementType Name="ModuleOptionResult">
      <Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
      <Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
  </Function>

以下是我为解决上述问题所采取的步骤。

  1. 删除.designer.cs文件
  2. 将以上标记添加到.dbml文件
  3. 排除.dbml和.dbml.layout文件
  4. 包含.dbml和.dbml.layout文件(这将再次生成.designer.cs文件,但不会将其包含在项目中)。
  5. 在项目中包含.designer文件。
  6. 获取模块类型和ModuleOption类型列表,如下所示。

  7. var modules = from row in results.GetResult<ModuleResult>().ToList()
                           select new Module
                                    {
                                      ModuleId = row.ModuleId,
                                      ModuleName = row.ModuleName,
                                      Description = row.Description,
                                      SalesDesc = row.SalesDesc,
                                      ParentModuleId = row.ParentModuleId
                                    };
    
    var moduleOptions = from row in results.GetResult<ModuleOptionResult>().ToList()
                        select new ModuleOption
                        {
                          ModuleOptionId = row.ModuleOptionId,
                          ModuleOptionName = row.ModuleOptionName,
                          ModuleOptionDesc = row.ModuleOptionDesc,
                          DefaultPrice = row.DefaultPrice,
                          ModuleId = row.ModuleId,
                          InUse = row.InUse
                        };
    

    <强>更新
    还是一种更好的方式。 右键单击解决方案资源管理器中的dbml文件,然后选择打开方式。选择XML Editor,当您在visual studio中保存文件时,它会自动生成designer.cs文件。