繁体   English   中英

Linq To Sql,存储过程返回多个临时表

[英]Linq To Sql, stored procedure returns multiple temp tables

如何使用LINQ to SQL在存储过程中返回多个临时表? 我有一个存储过程,调用时返回三个临时表。 如何使用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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM