从存储过程返回多个表 [英] Returning multiple tables from a stored procedure

查看:423
本文介绍了从存储过程返回多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的winform应用程序,我有以下情形:

In my winform application I have the following scenario:

我想在一个事件的多个表。返回所有表为数据集单个服务器周期,或得到一个表的时间,并使用单独的服务器周期为每个表哪一个更好?有什么好处一个比另一个?

I want to get multiple tables on a single event. Returning all tables as dataset in single server cycle, or getting one table at time and using separate server cycle for each table which one is better? What are the advantages one over another?

推荐答案

通常的方式是让所有的一次。

The normal way is to get all at once.

只是构建你的 SELECT 的,你将有一个数据集摆满了桌子。

just construct your SELECT's and you will have a DataSet filled with all tables.

using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(myConnString))
{
    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
    {
        cmd.CommandText = "myMultipleTablesSP";
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;

        conn.Open();

        System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adapter.Fill(ds);

        conn.Close();
    }
}

例如,如果你回到2表中的SP,如:

if for example you return 2 tables in your SP, like:

SELECT * FROM [TableA];
SELECT * FROM [TableB];

您会访问该表为:

DataTable tableA = ds.Tables[0];
DataTable tableB = ds.Tables[1];

这篇关于从存储过程返回多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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