如何将存储过程中的多个结果存储到数据集中?

How do I store multiple results from a stored procedure into a dataset?(如何将存储过程中的多个结果存储到数据集中?)
本文介绍了如何将存储过程中的多个结果存储到数据集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 StoredProcedure 中的结果集合并到 ASP.NET 中的一个数据集中?

How do I combine to result sets from a StoredProcedure into one dataset in ASP.NET?

下面是我在asp.net中的代码

Below is my code in asp.net

SqlDataAdapter adap = new System.Data.SqlClient.SqlDataAdapter("sp_Home_MainBanner_TopStory",con);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
adap.SelectCommand.Parameters.AddWithValue("@rows", 9);

DataSet DS = new DataSet();

adap.Fill(DS, "Table1");
adap.Fill(DS, "Table2");

GridView1.DataSource = DS.Tables["Table2"];
GridView1.DataBind();

即使有两个适配器,我如何将结果合并到一个数据集中?

Even if there were two adapters, how could I combine the results into one dataset?

推荐答案

DataSet 包含表.对于上面的示例,如果您有两个 SqlDataAdapter,每个都调用一个存储过程并像上面那样存储它们.

A DataSet contains Tables. For your above example, if you had two SqlDataAdapters, each calling a stored procedure and stored them like you did above.

adapter1.Fill(DS, "Table1");
adapter2.Fill(DS, "Table2");

这将从您的第一个查询中获取表结果并将其作为 Table1 存储在 DataSet DS 中.然后它将在同一个数据集中存储另一个表(表 2).要访问这些表,请使用以下代码:

This will take the table results from your first query and store it in the DataSet DS as Table1. It will then store another Table (Table2) in the same DataSet. To access these tables you use the following code:

DS.Tables["Table1"]  //Or Table2, or whatever you name it during your Fill.

您已经有了正确的流程,您只需要查看 DataSet 的工作原理并决定如何调用您的信息.

You already have the right process, you just need to look up how a DataSet works and decide how you want to call your information.

但是,如果您想将结果合并到一个 DataTable 中,则需要遍历这些表并合并信息.

IF you want to combine your results into one DataTable however, you will need to iterate through the tables and combine information.

ex:
DataTable combinedTable = new DataTable();
//Create columns

foreach (DataRow row in DS.Tables["Table1"].Rows)
{
    //Create rows?  Copy information over?  Whatever you want to do.
}

这篇关于如何将存储过程中的多个结果存储到数据集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)