如何存放在存储过程多个结果变成数据集? [英] How do I store multiple results from a stored procedure into a dataset?

查看:291
本文介绍了如何存放在存储过程多个结果变成数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何合并到结果集从一个StoredProcedure的进入一个数据集在ASP.NET?

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

下面是我的code在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中包含的表。为了您上面的例子中,如果您有两个SqlDataAdapters,每次调用存储过程和存储他们像上面一样。

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");

这会从第一个查询表结果,并将其存储在DataSet DS作为表1​​。然后,它将存储其他表(表2)在同一数据集。要访问您使用以下code这些表:

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.

如果你想你的结果合并成一个数据表但是,您将需要遍历表和合并的信息。

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.
}

这篇关于如何存放在存储过程多个结果变成数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆