从SQL Server存储过程返回多个数据集 [英] Return multiple datasets from sql server stored procedure
问题描述
我需要通过调用运行5个不同查询的存储过程,通过Web Api返回Base64 XML输出.
I need to return through Web Api a Base64 XML output based upon calling a stored procedures which runs 5 different queries.
未编写存储过程(我需要编写存储过程),但是有5个查询中的数据是完全不同的表和列等.所以我想知道这是否可能吗?
Stored procedure is not written ( I need to write it ) but there are 5 queries in which the data is completely different tables and columns etc... so I am wondering if this is even possible?
我知道在Oracle中可以返回多个游标,但是使用SQL Server时,我可以将多个数据集或集合返回到asp.net 4.5(mvc c#/Ado.net)吗?有什么例子吗?
I know in Oracle you can return multiple cursors, but with SQL Server , can I return into asp.net 4.5 ( mvc c# / Ado.net) multiple datasets or collections? Any examples of this?
仅查询一个示例
-- Content Tab
SELECT -- vTC.[TemplateId]
t.Name as "Client Name and Document" ,vTC.[SectionName] ,vTC.[ContentId] ,vTC.[ContentName]
,vTC.[ContentDescription],vTC.[ContentValue] ,CAL.ContentValue as "Spanish Content" , iif(S.IsClientSection = 1, 'Global Section','Template Section') as "Global or Template Section"
,DT.Title as DataType ,iif(vTC.IsRequired = 1, 'Yes', 'No') as "Required" ,vTC.[DisplayType]
FROM [dbo].[vwTemplateContent] vTC
left join dbo.Template t on vTC.TemplateId = t.TemplateId
left join dbo.DataType DT on vTC.DataTypeId = dt.datatypeid
left join dbo.Section S on S.SectionID = vTC.SectionID
left join [dbo].[ContentAlternateLanguage] CAL on vTC.ContentId = CAL.ContentID
where vTC.templateid in (1)
order by DisplayOrder
推荐答案
如果要获取多个表,则必须将多个select语句写入存储过程,如下所示:
If you are going to get multiple tables then you have to write multiple select statements into your stored procedure like below:
CREATE PROCEDURE SPName
(
/*Declare your parameters*/
@parm1 dataType
)
AS
BEGIN
/*Write your select statements below*/
-- SELECT * FROM tblName
-- SELECT * FROM tblName2
END
您必须将这些记录填充到数据集中, DataSet 支持将多个表导入ADO.net.
You have to fill these records into your DataSet, DataSet supports multiple table into ADO.net.
请参考以下代码填充您的数据集:
Please refer below code to fill your DataSet:
SqlConnection con=new SqlConnection("YourConnection String");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds = new DataSet();
cmd = new SqlCommand("SPName", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@parm1", id);//if you have parameters.
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
此后,您可以使用以下不同的多个记录集
After this you can take advantage of different multiple recordsets using
ds.Tables[0]
ds.Tables[1]
..
希望它将对您有帮助
谢谢
这篇关于从SQL Server存储过程返回多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!