C# 从sql server存储过程返回多个数据集
我需要通过Web Api返回一个Base64 XML输出,该输出基于调用一个运行5个不同查询的存储过程 未编写存储过程我需要编写它,但有5个查询中的数据是完全不同的表和列等。。。所以我想知道这是否可能 我知道在Oracle中可以返回多个游标,但在SQL Server中,我可以返回到asp.net 4.5 mvc/Ado.net多个数据集或集合吗?有这样的例子吗 仅一个查询的示例C# 从sql server存储过程返回多个数据集,c#,asp.net,sql-server,stored-procedures,C#,Asp.net,Sql Server,Stored Procedures,我需要通过Web Api返回一个Base64 XML输出,该输出基于调用一个运行5个不同查询的存储过程 未编写存储过程我需要编写它,但有5个查询中的数据是完全不同的表和列等。。。所以我想知道这是否可能 我知道在Oracle中可以返回多个游标,但在SQL Server中,我可以返回到asp.net 4.5 mvc/Ado.net多个数据集或集合吗?有这样的例子吗 仅一个查询的示例 -- Content Tab SELECT -- vTC.[TemplateId] t.Name as
-- 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查询,就可以在数据集中获得数据。如果只有一个select查询,那么将得到DataTable,如果select查询的数量为5,那么将得到一个包含5个DataTables的数据集。这很简单。只要写下你的程序,玩得开心 编辑: 下面给出了存储过程伪代码的示例:
create Proc Name_Of_Proc
(
@FirstParam DataType,
@SecondParam DataType
)
AS
Begin
Select statement 1
Select statement 2
Select statement 3 --and so on upto n.
end
您需要在数据库中执行此操作。完成此操作后,需要使用ADO.NET从c执行此过程。您需要使用SqlConnection SqlCommand和SqlDataReader对象来执行此操作。你可以在谷歌上搜索更多的例子。SO上的一个这样的链接是如果要获得多个表,则必须将多个select语句写入存储过程,如下所示:
CREATE PROCEDURE SPName
(
/*Declare your parameters*/
@parm1 dataType
)
AS
BEGIN
/*Write your select statements below*/
-- SELECT * FROM tblName
-- SELECT * FROM tblName2
END
您必须将这些记录填充到数据集中,支持将多个表填充到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]
..
希望对你有帮助
谢谢这里有一个基本的例子: SQL过程: C功能: C用法:
注意,除了返回的数据类型是DataSet而不是DataTable之外,它几乎与用于单个表存储过程的代码相同。数据集包含DataTableCollection如何编写存储过程和c ado.net?创建存储过程存储过程名称。。。。从表a中选择a、b、c、d、e从表b中选择z、h、m、i从表c中选择y、u、a、bb
CREATE PROCEDURE usp_getStudentsAndClasses
@ClassName varchar(50)
, @IsActive bit
AS
BEGIN
--First select is first table
SELECT *
FROM Students
--Second select is second table, etc.
SELECT *
FROM Classes
--Third table...
--Can be more complex, as long as there is a result set
SELECT s.FirstName
, s.LastName
FROM Students s
JOIN StudentSeating ss
ON s.StudentID = ss.StudentID
JOIN Classes c
ON c.ClassID = ss.ClassID
WHERE s.IsActive = @IsActive
AND c.Name = @ClassName
END
public DataSet GetDataSet(SqlConnection connection, string storedProcName, params SqlParameter[] parameters)
{
var command = new SqlCommand(storedProcName, connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddRange(parameters);
var result = new DataSet();
var dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(result);
return result;
}
var connection = new SqlConnection("Your_connection_string");
var parameters = new SqlParameter[]
{
new SqlParameter("ClassName", "Robotics"), //example of string value
new SqlParameter("IsActive", true) //example of numeric value
};
var dataSet = GetDataSet(connection, "usp_getStudentsAndClasses", parameters);
var firstTable = dataSet?.Tables?[0]; //use as any other data table...