我需要通过 Web Api 返回基于调用运行 5 个不同查询的存储过程的 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
如果您要获取多个表,那么您必须将多个选择语句写入存储过程,如下所示:
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]
..
希望对你有帮助
谢谢
SQL 过程:
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
C# 函数:
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;
}
C# 用法:
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...
注意,它与用于单表存储过程的代码几乎相同,只是返回的数据类型是
DataSet
,而不是
DataTable
。
DataSet
包含
DataTableCollection
MSDN 上的更多信息
DataSet
中获得数据。如果您有一个选择查询,您将得到
DataTable
,如果您有多个选择查询(比如 5 个),那么您将得到一个有 5 个
DataSet
的
DataTable
。就是这么简单。只需编写您的程序即可享受乐趣。
编辑: 存储过程示例(伪代码)如下:
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
对象来执行此操作。您可以在 google 或 SO 本身上搜索更多示例。 SO 上的这样一个链接是如何在 c# 中执行存储过程
Dapper
QueryMultipleAsync()
var query = "Select * from Table1 ";
query += "Select * from Table2";
or
query = "proc with multiple select.."
using (var res = await connection.QueryMultipleAsync(query, new { param1 =
param1}, transaction, commandTimeout, CommandType.Text/CommandType.StoredProcedure ))
{
return new T
{
Data1 = await res.ReadAsync<T1>(),
Data2 = await res.ReadAsync<T2>()
};
}