7 c# sql-server asp.net stored-procedures
我需要通过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
Run Code Online (Sandbox Code Playgroud)
Sun*_*mar 15
如果要获得多个表,则必须在存储过程中编写多个select语句,如下所示:
CREATE PROCEDURE SPName
(
/*Declare your parameters*/
@parm1 dataType
)
AS
BEGIN
/*Write your select statements below*/
-- SELECT * FROM tblName
-- SELECT * FROM tblName2
END
Run Code Online (Sandbox Code Playgroud)
您必须将这些记录填充到您的DataSet中,DataSet支持多个表进入ADO.net.
请参考以下代码填写您的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();
Run Code Online (Sandbox Code Playgroud)
在此之后,您可以使用不同的多个记录集
ds.Tables[0]
ds.Tables[1]
..
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你
谢谢
这是一个基本示例:
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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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...
Run Code Online (Sandbox Code Playgroud)
请注意,它几乎与用于单表存储过程的代码相同,只是返回的数据类型是 a DataSet,而不是 a DataTable。ADataSet包含MSDN 上的DataTableCollection 更多信息
| 归档时间: |
|
| 查看次数: |
21076 次 |
| 最近记录: |