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. SQL: getting and storing data

SQL: getting and storing data

Scheduled Pinned Locked Moved C#
databasequestiondata-structures
4 Posts 3 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.
  • G Offline
    G Offline
    Glen Harvy 0
    wrote on last edited by
    #1

    Hi, Best way is to show you what I want: SQL Database: SqlConnection connection = new SqlConnection(connectionString); SqlCommand getTableList = connection.CreateCommand(); string command = "Select name from sysobjects where type = 'U'"; getTableList.CommandText = command; connection.Open(); **string tables = getTableList.ExecuteScalar().ToString();** connection.Close(); MessageBox.Show("We got ..\n" + tables); How do I store the results of the SQL Command? The command appears to work OK but only the name of the first table is recorded in "tables". Ideally I would like to put the results of the SQL command into an array. TIA.

    Glen Harvy

    K 1 Reply Last reply
    0
    • G Glen Harvy 0

      Hi, Best way is to show you what I want: SQL Database: SqlConnection connection = new SqlConnection(connectionString); SqlCommand getTableList = connection.CreateCommand(); string command = "Select name from sysobjects where type = 'U'"; getTableList.CommandText = command; connection.Open(); **string tables = getTableList.ExecuteScalar().ToString();** connection.Close(); MessageBox.Show("We got ..\n" + tables); How do I store the results of the SQL Command? The command appears to work OK but only the name of the first table is recorded in "tables". Ideally I would like to put the results of the SQL command into an array. TIA.

      Glen Harvy

      K Offline
      K Offline
      KaineDunno
      wrote on last edited by
      #2

      Glen, You only get the first table because you're using the executeScalar method. To get what you want you need to use the executeReader. Your code would thus be smth like: SqlConnection connection = new SqlConnection(connectionString); string command = "Select name From sysobjects where type ='U'"; SqlCommand getTableList = new SqlCommand(command, connection); myConnection.Open(); SqlDataReader reader = getTableList.ExecuteReader(CommandBehavior.CloseConnection); List tables=new List(); while(reader.Read()) { tables.Add(reader.GetString(0)); } reader.Close(); //Implicitly closes the connection because CommandBehavior.CloseConnection was specified. This should get you the list. Greeting Kaine

      K G 2 Replies Last reply
      0
      • K KaineDunno

        Glen, You only get the first table because you're using the executeScalar method. To get what you want you need to use the executeReader. Your code would thus be smth like: SqlConnection connection = new SqlConnection(connectionString); string command = "Select name From sysobjects where type ='U'"; SqlCommand getTableList = new SqlCommand(command, connection); myConnection.Open(); SqlDataReader reader = getTableList.ExecuteReader(CommandBehavior.CloseConnection); List tables=new List(); while(reader.Read()) { tables.Add(reader.GetString(0)); } reader.Close(); //Implicitly closes the connection because CommandBehavior.CloseConnection was specified. This should get you the list. Greeting Kaine

        K Offline
        K Offline
        Keshav V Kamat 0
        wrote on last edited by
        #3

        I agree fully with Kaine. just an alternate method when using a reader.

        SqlDataReader reader = getTableList.ExecuteReader();

        and then continue to add to the tables, while using reader.Read().

        Keshav Kamat :) India

        1 Reply Last reply
        0
        • K KaineDunno

          Glen, You only get the first table because you're using the executeScalar method. To get what you want you need to use the executeReader. Your code would thus be smth like: SqlConnection connection = new SqlConnection(connectionString); string command = "Select name From sysobjects where type ='U'"; SqlCommand getTableList = new SqlCommand(command, connection); myConnection.Open(); SqlDataReader reader = getTableList.ExecuteReader(CommandBehavior.CloseConnection); List tables=new List(); while(reader.Read()) { tables.Add(reader.GetString(0)); } reader.Close(); //Implicitly closes the connection because CommandBehavior.CloseConnection was specified. This should get you the list. Greeting Kaine

          G Offline
          G Offline
          Glen Harvy 0
          wrote on last edited by
          #4

          Thansk for that - much appreciated.

          Glen Harvy

          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