Returning multiple tables from a stored procedure

后端 未结 2 1247
青春惊慌失措
青春惊慌失措 2020-12-03 04:42

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 sin

相关标签:
2条回答
  • 2020-12-03 05:08

    The normal way is to get all at once.

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

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

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

    you would access this tables as:

    DataTable tableA = ds.Tables[0];
    DataTable tableB = ds.Tables[1];
    
    0 讨论(0)
  • 2020-12-03 05:29

    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.

    0 讨论(0)
提交回复
热议问题