从存储过程返回多个表

时间:2023-02-04 16:36:15

In my winform application I have the following scenario:

在我的winform应用程序中,我有以下场景:

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?

我想在一个事件上得到多个表。在单个服务器周期中返回所有表作为数据集,或者每次只获取一个表,并对每个表使用单独的服务器周期,哪个更好?有什么优势?

2 个解决方案

#1


45  

The normal way is to get all at once.

通常的方法是一次性完成所有任务。

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

只要构造SELECT's,就会有一个包含所有表的数据集。

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();
    }
}

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

例如,如果您返回SP中的两个表,例如:

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

you would access this tables as:

你可按以下方式进入本表格:

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

#2


1  

If you load each table separately and use threads you can greatly improve the performance.

如果您分别加载每个表并使用线程,您可以极大地提高性能。

Datasets are also very heavy weight... so try avoiding them if possible.

数据集也很重…所以如果可能的话,尽量避免。

#1


45  

The normal way is to get all at once.

通常的方法是一次性完成所有任务。

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

只要构造SELECT's,就会有一个包含所有表的数据集。

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();
    }
}

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

例如,如果您返回SP中的两个表,例如:

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

you would access this tables as:

你可按以下方式进入本表格:

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

#2


1  

If you load each table separately and use threads you can greatly improve the performance.

如果您分别加载每个表并使用线程,您可以极大地提高性能。

Datasets are also very heavy weight... so try avoiding them if possible.

数据集也很重…所以如果可能的话,尽量避免。