0

I have a SQL Server stored procedure that has 3 selects and returns 3 different data tables. I want to save all these tables into their own Viewbags or ViewData in ASP.NET MVC using C#

Calling the SQL Server stored procedure:

exec getData 1, '#244'

This returns 3 rows with different row and column numbers from grouping the data

C# model class - everything has been initialized

public void financeData(int id, string purchase_number)
{
        con.ConnectionString = constr;
        string query = "getData";

        SqlCommand command = new SqlCommand(query, con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@id", id);
        command.Parameters.AddWithValue("@purchase_number", purchase_number);

        DataTable top_purchases = new DataTable();
        DataTable transaction_summary = new DataTable();
        DataTable maximum_amount = new DataTable();

        // code to read the tables into different lists
        con.Open();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
               // code to read the tables into different lists
            }
        }
}

Is there a way to use one procedure for the different data sets since I need to add more procedures with similar queries?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Klaus
  • 19
  • 4
  • 2
    Does this answer your question? [Return multiple datasets from sql server stored procedure](https://stackoverflow.com/questions/40013747/return-multiple-datasets-from-sql-server-stored-procedure) – SMor Jun 26 '22 at 18:22
  • Side note: Your connection and command need `using`, do *not* cache the connection object. – Charlieface Jun 26 '22 at 20:12

0 Answers0