Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Retrieve Data from Stored Proc

Retrieve Data from Stored Proc

Scheduled Pinned Locked Moved Database
csharptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikasa
    wrote on last edited by
    #1

    Ok, does anyone know how to retrieve Data that is generated from a Stored proc into an ADO.NET DataSet or DataTable? This used to be very easy in VB6... :wtf: I just need a sample to lead me on the right track.

    C M 2 Replies Last reply
    0
    • M mikasa

      Ok, does anyone know how to retrieve Data that is generated from a Stored proc into an ADO.NET DataSet or DataTable? This used to be very easy in VB6... :wtf: I just need a sample to lead me on the right track.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      In the stored procedure you just SELECT the relevant data. In the .NET application open a SqlConnection, create a SqlCommand to call the Stored Procedure and then call SqlCommand.ExecuteQuery() to get a DataReader to extract it* Does this help? *You can also uses crazy things like DataAdapters and DataSets, but y'know I don't really like them


      "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

      M 1 Reply Last reply
      0
      • C Colin Angus Mackay

        In the stored procedure you just SELECT the relevant data. In the .NET application open a SqlConnection, create a SqlCommand to call the Stored Procedure and then call SqlCommand.ExecuteQuery() to get a DataReader to extract it* Does this help? *You can also uses crazy things like DataAdapters and DataSets, but y'know I don't really like them


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

        M Offline
        M Offline
        mikasa
        wrote on last edited by
        #3

        Sorry, I should have been more specific. Unfortunately, I do need to get this data into a DataTable or DataSet. Anyone have an idea? I know the DataAdapter uses a DataReader internally, but I don't know how I could implement this...I'm sure it would be super difficult.

        R 1 Reply Last reply
        0
        • M mikasa

          Sorry, I should have been more specific. Unfortunately, I do need to get this data into a DataTable or DataSet. Anyone have an idea? I know the DataAdapter uses a DataReader internally, but I don't know how I could implement this...I'm sure it would be super difficult.

          R Offline
          R Offline
          rudy net
          wrote on last edited by
          #4

          In the "Server Explorer" window select the connection to your database that contains the stored procedure and drag it to your form. This will create a SqlConnection object and a SqlCommand object, for example conn and cmd. Then you can fill your DataSet with the following commands: conn.Open(); SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet("datasetName"); dataAdapter.Fill(dataset); conn.Close();

          M 1 Reply Last reply
          0
          • R rudy net

            In the "Server Explorer" window select the connection to your database that contains the stored procedure and drag it to your form. This will create a SqlConnection object and a SqlCommand object, for example conn and cmd. Then you can fill your DataSet with the following commands: conn.Open(); SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet("datasetName"); dataAdapter.Fill(dataset); conn.Close();

            M Offline
            M Offline
            mikasa
            wrote on last edited by
            #5

            Yeah, that's not going to work either, tried that before... The problem is that the Stored Proc is Dynamically creating Columns and Data depending on what Parameters you pass to it. Even in the Designer, I can't get it to Generate a DataSet because ADO.NET what hard-defined structures of Data when working with the Designer. However, I can run the Data Preview and it works fine, but that is because in the "Preview" mode, the data is being retrieved through a DataReader, not a DataTable. Why can't the DataCommand Object just return a DataTable like it did in VB6!? :sigh:

            1 Reply Last reply
            0
            • M mikasa

              Ok, does anyone know how to retrieve Data that is generated from a Stored proc into an ADO.NET DataSet or DataTable? This used to be very easy in VB6... :wtf: I just need a sample to lead me on the right track.

              M Offline
              M Offline
              Michael Potter
              wrote on last edited by
              #6

              I have no trouble executing SPs like you descibe. Here is a sample of code I just wrote today:

              public static DataSet GetEventList(
                   int activeState, 
                   int eventId,
                   int eventDateStatus,
                   DateTime eventDateStart,
                   DateTime eventDateEnd,
                   int createDateStatus,
                   DateTime createDateStart,
                   DateTime createDateEnd,
                   int ntdStatus,
                   int eventTypeId,
                   string firstName,
                   string lastName)
              {
                   DataSet ds = null;
                   SqlCommand cmd = null;
                   SqlDataAdapter da = null;
              
                   SqlConnection conn = GetConnection();
                   if (conn != null)
                   {
                        try
                        {
                             cmd = new SqlCommand("EventListGet",conn);
                             cmd.CommandType = CommandType.StoredProcedure;
              
                             SqlParameter p = cmd.Parameters.Add("@ActiveState",SqlDbType.Int);
                             p.Value = activeState;
                                  
                             p = cmd.Parameters.Add("@EventId",SqlDbType.Int);
                             p.Value = eventId;
              
                             p = cmd.Parameters.Add("@EventDateStatus",SqlDbType.Int);
                             p.Value = eventDateStatus;
                                  
                             p = cmd.Parameters.Add("@EventDateStart",SqlDbType.DateTime);
                             p.Value = eventDateStart;
                                  
                             p = cmd.Parameters.Add("@EventDateEnd",SqlDbType.DateTime);
                             p.Value = eventDateEnd;
              
                             p = cmd.Parameters.Add("@CreateDateStatus",SqlDbType.Int);
                             p.Value = createDateStatus;
              					
                             p = cmd.Parameters.Add("@CreateDateStart",SqlDbType.DateTime);
                             p.Value = createDateStart;
              					
                             p = cmd.Parameters.Add("@CreateDateEnd",SqlDbType.DateTime);
                             p.Value = createDateEnd;
              
                             p = cmd.Parameters.Add("@NTDState",SqlDbType.Int);
                             p.Value = ntdStatus;
              
                             p = cmd.Parameters.Add("@EventTypeId",SqlDbType.Int);
                             p.Value = eventTypeId;
              
                             p = cmd.Parameters.Add("@FirstName",SqlDbType.VarChar,50);
                             p.Value = firstName;
              
                             p = cmd.Parameters.Add("@LastName",SqlDbType.VarChar,50);
                             p.Value = lastName;
              					
                             da = new SqlDataAdapter(cmd);
                             ds = new DataSet();
                             da.Fill(ds);
                        }
                        catch (Exception ex)
                        {
                             MessageBox.Show(ex.Message,"Load Event List Failed");
                             if (ds != null)
                             {
                                  ds.Dispo
              
              M 1 Reply Last reply
              0
              • M Michael Potter

                I have no trouble executing SPs like you descibe. Here is a sample of code I just wrote today:

                public static DataSet GetEventList(
                     int activeState, 
                     int eventId,
                     int eventDateStatus,
                     DateTime eventDateStart,
                     DateTime eventDateEnd,
                     int createDateStatus,
                     DateTime createDateStart,
                     DateTime createDateEnd,
                     int ntdStatus,
                     int eventTypeId,
                     string firstName,
                     string lastName)
                {
                     DataSet ds = null;
                     SqlCommand cmd = null;
                     SqlDataAdapter da = null;
                
                     SqlConnection conn = GetConnection();
                     if (conn != null)
                     {
                          try
                          {
                               cmd = new SqlCommand("EventListGet",conn);
                               cmd.CommandType = CommandType.StoredProcedure;
                
                               SqlParameter p = cmd.Parameters.Add("@ActiveState",SqlDbType.Int);
                               p.Value = activeState;
                                    
                               p = cmd.Parameters.Add("@EventId",SqlDbType.Int);
                               p.Value = eventId;
                
                               p = cmd.Parameters.Add("@EventDateStatus",SqlDbType.Int);
                               p.Value = eventDateStatus;
                                    
                               p = cmd.Parameters.Add("@EventDateStart",SqlDbType.DateTime);
                               p.Value = eventDateStart;
                                    
                               p = cmd.Parameters.Add("@EventDateEnd",SqlDbType.DateTime);
                               p.Value = eventDateEnd;
                
                               p = cmd.Parameters.Add("@CreateDateStatus",SqlDbType.Int);
                               p.Value = createDateStatus;
                					
                               p = cmd.Parameters.Add("@CreateDateStart",SqlDbType.DateTime);
                               p.Value = createDateStart;
                					
                               p = cmd.Parameters.Add("@CreateDateEnd",SqlDbType.DateTime);
                               p.Value = createDateEnd;
                
                               p = cmd.Parameters.Add("@NTDState",SqlDbType.Int);
                               p.Value = ntdStatus;
                
                               p = cmd.Parameters.Add("@EventTypeId",SqlDbType.Int);
                               p.Value = eventTypeId;
                
                               p = cmd.Parameters.Add("@FirstName",SqlDbType.VarChar,50);
                               p.Value = firstName;
                
                               p = cmd.Parameters.Add("@LastName",SqlDbType.VarChar,50);
                               p.Value = lastName;
                					
                               da = new SqlDataAdapter(cmd);
                               ds = new DataSet();
                               da.Fill(ds);
                          }
                          catch (Exception ex)
                          {
                               MessageBox.Show(ex.Message,"Load Event List Failed");
                               if (ds != null)
                               {
                                    ds.Dispo
                
                M Offline
                M Offline
                mikasa
                wrote on last edited by
                #7

                Ok, this is very similar to what I tried. Do you think it matters if the Stored Proc is creating and using a Temporary Table (ex. ##TempTable)? I get errors on my Temp Table in the Designer. It doesn't make any sense because I can run this in the Query Analyzer without fail every time!

                M 1 Reply Last reply
                0
                • M mikasa

                  Ok, this is very similar to what I tried. Do you think it matters if the Stored Proc is creating and using a Temporary Table (ex. ##TempTable)? I get errors on my Temp Table in the Designer. It doesn't make any sense because I can run this in the Query Analyzer without fail every time!

                  M Offline
                  M Offline
                  Michael Potter
                  wrote on last edited by
                  #8

                  Are you sure the table is being DROP'ed properly at the end of your SP? What is the error you are getting?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups