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. General Programming
  3. C#
  4. Give name to table from stored procedure

Give name to table from stored procedure

Scheduled Pinned Locked Moved C#
databasetutorialquestion
4 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.
  • L Offline
    L Offline
    laphijia
    wrote on last edited by
    #1

    I've built a fairly complex stored procedure that has a couple of selects in it that are only functional while I'm only interested in reading the resultset from the last select. Now when I fill my dataset, apart from retrieving useless data from the database, I have to acess my data doing something like ds.Tables[2].Rows I have thought of two possible solutions even though I don't know how to implement any of them. 1. Have the stored procedure return only the last recordset. If this is possible I'm missing some T-SQL command. Anybody on this? 2. Give a name to the table that I'm interested in reading so that I could access it disregarding it's index like ds.Tables["MyRecordset"].Rows This is much more elegant than accessing with numeric index bacause if I change the stored procedure I don't have to change my code. Anyway how does one give a name to a table from a stored procedure? Thanks Edd

    R G H 3 Replies Last reply
    0
    • L laphijia

      I've built a fairly complex stored procedure that has a couple of selects in it that are only functional while I'm only interested in reading the resultset from the last select. Now when I fill my dataset, apart from retrieving useless data from the database, I have to acess my data doing something like ds.Tables[2].Rows I have thought of two possible solutions even though I don't know how to implement any of them. 1. Have the stored procedure return only the last recordset. If this is possible I'm missing some T-SQL command. Anybody on this? 2. Give a name to the table that I'm interested in reading so that I could access it disregarding it's index like ds.Tables["MyRecordset"].Rows This is much more elegant than accessing with numeric index bacause if I change the stored procedure I don't have to change my code. Anyway how does one give a name to a table from a stored procedure? Thanks Edd

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      If you are using a DataAdapter to fill you dataset you can always create a strong typed dataset which allows you to access the data as: myDs.MyTableName[row].MyField; or dsMyStrongType.MyStrongTypeRow dr = myDs.MyTableName[row]; dr.MyField = something Makes life a lot easier and less apt to have typos on field names (plus you get Intellesense). Just build your DataAdapter in the editor and the right click on it to generate a dataset. If your stored procedure returns multiple results it will build a table by name for each. You can also select the table mapping property to call the tables anything you like. Really cool stuff. Rocky <>< www.HintsAndTips.com

      1 Reply Last reply
      0
      • L laphijia

        I've built a fairly complex stored procedure that has a couple of selects in it that are only functional while I'm only interested in reading the resultset from the last select. Now when I fill my dataset, apart from retrieving useless data from the database, I have to acess my data doing something like ds.Tables[2].Rows I have thought of two possible solutions even though I don't know how to implement any of them. 1. Have the stored procedure return only the last recordset. If this is possible I'm missing some T-SQL command. Anybody on this? 2. Give a name to the table that I'm interested in reading so that I could access it disregarding it's index like ds.Tables["MyRecordset"].Rows This is much more elegant than accessing with numeric index bacause if I change the stored procedure I don't have to change my code. Anyway how does one give a name to a table from a stored procedure? Thanks Edd

        G Offline
        G Offline
        Guillermo Rivero
        wrote on last edited by
        #3

        Add an output parameter to the stored procedure and send the Table using it. Send me the query. Free your mind...

        1 Reply Last reply
        0
        • L laphijia

          I've built a fairly complex stored procedure that has a couple of selects in it that are only functional while I'm only interested in reading the resultset from the last select. Now when I fill my dataset, apart from retrieving useless data from the database, I have to acess my data doing something like ds.Tables[2].Rows I have thought of two possible solutions even though I don't know how to implement any of them. 1. Have the stored procedure return only the last recordset. If this is possible I'm missing some T-SQL command. Anybody on this? 2. Give a name to the table that I'm interested in reading so that I could access it disregarding it's index like ds.Tables["MyRecordset"].Rows This is much more elegant than accessing with numeric index bacause if I change the stored procedure I don't have to change my code. Anyway how does one give a name to a table from a stored procedure? Thanks Edd

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          When using DataSets, try designing a strongly-typed DataSet by using the DataSet designer in VS.NET (there are other ways and tools, too, but I present the way in VS.NET). Right-click on your project or subfolder and select Add New Item. Find DataSet, give it a name, and click OK. You'll see a component designer screen. Right-click to add elements (tables) and each element has rows and their types (the columns). You can even add relationships and primary keys. Just play around with it a little. You can even use the Server Explorer if you have a database connection to drag-n-drop tables or stored procedures which returns tables to generate these automatically. When you fill the strongly-typed DataSet, your DbDataAdapter derivative (like SqlDataAdapter) has to be configured to map the tables. This is done through DbDataAdapter.TableMappings and can easily be configured by using the data adapter designer in VS.NET as well. Finally, you don't even need to use strongly-typed if you use the TableMappings property mentioned above. When selecting multiple result sets, the sequence of table names is "Table", "Table1", "Table2", etc. If you map these to other names, you can use DataSet.Tables["_nameOfTable_"]. I recommend using strongly-typed DataSets, though. You can easily refer to columns by name and don't have to worry about casting since those named rows return the appropriate Type.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          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