问题描述
如何将 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.
}
这篇关于如何将存储过程中的多个结果存储到数据集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!