我需要通过调用运行5个不同查询的存储过程,通过Web Api返回Base64 XML输出。

存储过程不是写的(我需要写),但是有5个查询中的数据是完全不同的表和列等。所以我想知道这是否有可能?

我知道在Oracle中可以返回多个游标,但是使用SQL Server时,我可以返回asp.net 4.5(mvc C#/Ado.net)多个数据集或集合吗?有什么例子吗?

仅查询之一的示例

   -- 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语句写入存储过程,如下所示:

CREATE PROCEDURE SPName
(
/*Declare your parameters*/
@parm1 dataType
)
AS
BEGIN
/*Write your select statements below*/
-- SELECT * FROM tblName
-- SELECT * FROM tblName2

END

您必须将这些记录填充到DataSet中,DataSet支持将多个表插入ADO.net。

请引用以下代码以填充您的数据集:
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();

之后,您可以使用以下多个记录集
ds.Tables[0]
ds.Tables[1]
..

希望对您有帮助

谢谢

关于c# - 从SQL Server存储过程返回多个数据集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40013747/

10-12 22:35
查看更多