![](/img/trans.png)
[英]Reading into C# XML output from stored procedure returns null from TEMP tables SQL Server
[英]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.