Linq To Sql、存储过程 returns 多个临时表

Linq To Sql, stored procedure returns multiple temp tables

如何使用 LINQ to SQL 在存储过程中 return 多个临时表?我有一个存储过程,当调用returns 三个临时表时。如何使用 LINQ to SQL?

在 .NET C# 代码中访问临时表中的数据

我能够访问数据,下面是我遵循的方法

存储过程:

CREATE PROCEDURE [dbo].[DemoSP] 
AS
BEGIN
    DECLARE @Temp1 TABLE   
    (   
        DocumentID NVARCHAR(256),
        DocumentName VARCHAR(100)
    )  
    DECLARE @Temp2 TABLE   
    (   
        DocumentID NVARCHAR(256)
    )  
    DECLARE @Temp3 TABLE   
    (   
        DocumentID NVARCHAR(256)
    )  

    INSERT INTO @Temp1 VALUES('123','Description')
    INSERT INTO @Temp1 VALUES('456','Content')
    INSERT INTO @Temp1 VALUES('789','Summary')

    INSERT INTO @Temp2 VALUES('XYZ')
    INSERT INTO @Temp2 VALUES('ABC')

    INSERT INTO @Temp3 VALUES('123')

    SELECT * from @Temp1 
    SELECT * from @Temp2 
    SELECT * from @Temp3 
END

创建一个 dbml 文件,将存储过程拖放到设计图面上。此步骤将生成以下代码片段

global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.DemoSP")]
        public ISingleResult<DemoSPResult> DemoSP()
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
            return ((ISingleResult<DemoSPResult>)(result.ReturnValue));
        }

     public partial class DemoSPResult
     {
        private string _DocumentID;
        private string _DocumentName;
        public DemoSPResult()
        {
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentID", DbType = "NVarChar(256)")]
        public string DocumentID
        {
            get
            {
                return this._DocumentID;
            }
            set
            {
                if ((this._DocumentID != value))
                {
                    this._DocumentID = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentName", DbType = "VarChar(100)")]
        public string DocumentName
        {
            get
            {
                return this._DocumentName;
            }
            set
            {
                if ((this._DocumentName != value))
                {
                    this._DocumentName = value;
                }
            }
        }
    }

注释上述属性,创建 类 具有与临时表架构相匹配的属性

     class Temp1
     {
        private string _DocumentID;
        private string _DocumentName;

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentID", DbType = "NVarChar(256)")]
        string DocumentID
        {
            get
            {
                return this._DocumentID;
            }
            set
            {
                if ((this._DocumentID != value))
                {
                    this._DocumentID = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentName", DbType = "VarChar(100)")]
        string DocumentName
        {
            get
            {
                return this._DocumentName;
            }
            set
            {
                if ((this._DocumentName != value))
                {
                    this._DocumentName = value;
                }
            }
        }
    }

    class Temp2
    {
        private string _DocumentID ;

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentID ", DbType = "NVARCHAR(256)")]
        string DocumentID 
        {
            get
            {
                return this._DocumentID ;
            }
            set
            {
                if ((this._DocumentID != value))
                {
                    this._DocumentID = value;
                }
            }
        }
    }

    class Temp3
    {
        private string _DocumentID;

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_DocumentID", DbType = "NVarChar(256)")]
        string DocumentID
        {
            get
            {
                return this._DocumentID;
            }
            set
            {
                if ((this._DocumentID != value))
                {
                    this._DocumentID = value;
                }
            }
        }
    }

创建一个方法来处理存储过程返回的多个结果集,如下所示

[Function(Name = "dbo.DemoSP")]
[ResultType(typeof(Temp1))]
[ResultType(typeof(Temp2))]
[ResultType(typeof(Temp3))]
public IMultipleResults GetDocumentDetails()
{
      IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
      return (IMultipleResults)result.ReturnValue;
}

我们可以调用上面的方法访问数据

 IMultipleResults d = db.GetDocumentDetails();

 IList<Temp1> temptable1= d.GetResult<Temp1>().ToList();
 IList<Temp2> temptable2= d.GetResult<Temp2>().ToList();
 IList<Temp3> temptable3= d.GetResult<Temp3>().ToList();