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. Assigning a SQL query result to a variable

Assigning a SQL query result to a variable

Scheduled Pinned Locked Moved C#
databasesecurityhelpquestion
6 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.
  • J Offline
    J Offline
    JohnQuar1
    wrote on last edited by
    #1

    Hello, I have a very simple query SELECT newid(), and I want to take the result of this query and assign it a variable so I can use it as a unique ID and use that again in another query. I am having trouble saving the actual variable. Can anyone help? Here is what I have so far. I am also pretty sure that my ID.Close() is in the wrong location. Let me kno what you think.

            Session\["userName"\] = "JMQ";
            // create a new SqlConnection object with the appropriate connection string 
            SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost\_Model;Integrated Security=True");
    
            //Open the connection
            sqlConn.Open();
    
            String result;
    
            //Create unique ID
            SqlCommand sqlComm1 = new SqlCommand("SELECT newid()", sqlConn);
            SqlDataReader ID = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
            
            //Close the connection
            ID.Close();
            
            sqlConn.Open();
            SqlCommand sqlComm2 = new SqlCommand(
                "SELECT \* INTO ##tempFAR15\_" + Session\["userName"\] + "\_" + ID + " FROM FN\_FAR15(1)",
                sqlConn);
            SqlDataReader r = sqlComm2.ExecuteReader(CommandBehavior.CloseConnection);
           
            r.Close();
            
            Response.Redirect("FAR15.aspx");
    

    I want to take the result from the 1st query which I (probably innapropriately have labeled as ID) and then use ID in teh second query. Thanks ALOT!!

    C P 2 Replies Last reply
    0
    • J JohnQuar1

      Hello, I have a very simple query SELECT newid(), and I want to take the result of this query and assign it a variable so I can use it as a unique ID and use that again in another query. I am having trouble saving the actual variable. Can anyone help? Here is what I have so far. I am also pretty sure that my ID.Close() is in the wrong location. Let me kno what you think.

              Session\["userName"\] = "JMQ";
              // create a new SqlConnection object with the appropriate connection string 
              SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost\_Model;Integrated Security=True");
      
              //Open the connection
              sqlConn.Open();
      
              String result;
      
              //Create unique ID
              SqlCommand sqlComm1 = new SqlCommand("SELECT newid()", sqlConn);
              SqlDataReader ID = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
              
              //Close the connection
              ID.Close();
              
              sqlConn.Open();
              SqlCommand sqlComm2 = new SqlCommand(
                  "SELECT \* INTO ##tempFAR15\_" + Session\["userName"\] + "\_" + ID + " FROM FN\_FAR15(1)",
                  sqlConn);
              SqlDataReader r = sqlComm2.ExecuteReader(CommandBehavior.CloseConnection);
             
              r.Close();
              
              Response.Redirect("FAR15.aspx");
      

      I want to take the result from the 1st query which I (probably innapropriately have labeled as ID) and then use ID in teh second query. Thanks ALOT!!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Use ExecuteScalar when only one piece of data is returned.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      J 1 Reply Last reply
      0
      • C Christian Graus

        Use ExecuteScalar when only one piece of data is returned.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        J Offline
        J Offline
        JohnQuar1
        wrote on last edited by
        #3

        I thought about that but I am not entirely sure how to use it. Can you give me an example?

        1 Reply Last reply
        0
        • J JohnQuar1

          Hello, I have a very simple query SELECT newid(), and I want to take the result of this query and assign it a variable so I can use it as a unique ID and use that again in another query. I am having trouble saving the actual variable. Can anyone help? Here is what I have so far. I am also pretty sure that my ID.Close() is in the wrong location. Let me kno what you think.

                  Session\["userName"\] = "JMQ";
                  // create a new SqlConnection object with the appropriate connection string 
                  SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost\_Model;Integrated Security=True");
          
                  //Open the connection
                  sqlConn.Open();
          
                  String result;
          
                  //Create unique ID
                  SqlCommand sqlComm1 = new SqlCommand("SELECT newid()", sqlConn);
                  SqlDataReader ID = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
                  
                  //Close the connection
                  ID.Close();
                  
                  sqlConn.Open();
                  SqlCommand sqlComm2 = new SqlCommand(
                      "SELECT \* INTO ##tempFAR15\_" + Session\["userName"\] + "\_" + ID + " FROM FN\_FAR15(1)",
                      sqlConn);
                  SqlDataReader r = sqlComm2.ExecuteReader(CommandBehavior.CloseConnection);
                 
                  r.Close();
                  
                  Response.Redirect("FAR15.aspx");
          

          I want to take the result from the 1st query which I (probably innapropriately have labeled as ID) and then use ID in teh second query. Thanks ALOT!!

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Why not just use System.Guid newid = System.Guid.NewGuid() ; ? That's one of the benefits of GUIDs over identities.

          J 1 Reply Last reply
          0
          • P PIEBALDconsult

            Why not just use System.Guid newid = System.Guid.NewGuid() ; ? That's one of the benefits of GUIDs over identities.

            J Offline
            J Offline
            JohnQuar1
            wrote on last edited by
            #5

            That was the perfect answer. Did exactly what I needed. Thank you!

            P 1 Reply Last reply
            0
            • J JohnQuar1

              That was the perfect answer. Did exactly what I needed. Thank you!

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Glad to be of service.

              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