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. Need to parse GUID

Need to parse GUID

Scheduled Pinned Locked Moved C#
helpdatabasesecurityquestion
6 Posts 2 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 am creating a GUID and then using that ID inside a SQL statement. I get an error saying that incorrect syntax near '-' , and I believe its referring to the dashes that are created with the GUID. So i was going to split up the GUID and just make it a string of characters. Can anyone help me do this? BElow is a little bit of code to show how I'm using it. Thanks for your help!

            SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost\_Model;Integrated Security=True");
    
            //Open the connection
            sqlConn.Open();
    
            //Create unique ID
            System.Guid UID = System.Guid.NewGuid();
    
            //Create Scenario ID
            Int16 SCENARIOID = 1;
    
            SqlCommand sqlComm1 = new SqlCommand(
            //SCENARIOID needs to be taken from the user selection when creating/modifying a scenario (must be globally available for all modules)
            "INSERT INTO web\_far15 select " + UID + ", \* from fn\_far15(" + SCENARIOID + ")",
                sqlConn);
                //"SELECT \* INTO TempTest\_1 FROM FN\_FAR15(1)", //##tempFAR15\_" + Session\["userName"\] + "\_" + UID + " FROM FN\_FAR15(1)",
                //sqlConn);
            SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
    
            Session\["TableName"\] = "web\_far15";//tempFAR15\_" + Session\["userName"\] + "\_" + UID;
            Session\["SessionID"\] = UID;
    
            r.Close();
    

    Thanks again for your help!

    P 1 Reply Last reply
    0
    • J JohnQuar1

      Hello, I am creating a GUID and then using that ID inside a SQL statement. I get an error saying that incorrect syntax near '-' , and I believe its referring to the dashes that are created with the GUID. So i was going to split up the GUID and just make it a string of characters. Can anyone help me do this? BElow is a little bit of code to show how I'm using it. Thanks for your help!

              SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost\_Model;Integrated Security=True");
      
              //Open the connection
              sqlConn.Open();
      
              //Create unique ID
              System.Guid UID = System.Guid.NewGuid();
      
              //Create Scenario ID
              Int16 SCENARIOID = 1;
      
              SqlCommand sqlComm1 = new SqlCommand(
              //SCENARIOID needs to be taken from the user selection when creating/modifying a scenario (must be globally available for all modules)
              "INSERT INTO web\_far15 select " + UID + ", \* from fn\_far15(" + SCENARIOID + ")",
                  sqlConn);
                  //"SELECT \* INTO TempTest\_1 FROM FN\_FAR15(1)", //##tempFAR15\_" + Session\["userName"\] + "\_" + UID + " FROM FN\_FAR15(1)",
                  //sqlConn);
              SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
      
              Session\["TableName"\] = "web\_far15";//tempFAR15\_" + Session\["userName"\] + "\_" + UID;
              Session\["SessionID"\] = UID;
      
              r.Close();
      

      Thanks again for your help!

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

      Ummm... what? If you provide the GUID as a string SQL Server will parse it. SELECT * FROM table WHERE ID='blah-blah-blah...' I don't know whether or not other databases will. But you'd have an even easier time using a parameterized statement.

      J 1 Reply Last reply
      0
      • P PIEBALDconsult

        Ummm... what? If you provide the GUID as a string SQL Server will parse it. SELECT * FROM table WHERE ID='blah-blah-blah...' I don't know whether or not other databases will. But you'd have an even easier time using a parameterized statement.

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

        All I did to get the GUID was System.Guid UID = System.Guid.NewGuid(); `which created the code. When I try to pass the GUID directly it does not allow it, I get the '-' error explained above. Its also not letting me typecast it to a string.`

        P 1 Reply Last reply
        0
        • J JohnQuar1

          All I did to get the GUID was System.Guid UID = System.Guid.NewGuid(); `which created the code. When I try to pass the GUID directly it does not allow it, I get the '-' error explained above. Its also not letting me typecast it to a string.`

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

          Parse it with what? Why do you need to parse it? System.Guid has a constructor that will do it. SQL Server will do it. But you shouldn't need to do it at all.

          J 1 Reply Last reply
          0
          • P PIEBALDconsult

            Parse it with what? Why do you need to parse it? System.Guid has a constructor that will do it. SQL Server will do it. But you shouldn't need to do it at all.

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

            I believe I need to parse it so I can take out the dashes. When I run the code, I get the error "Incorrect syntax near '-'." and it breaks at SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection); So when I read that, that sounds like I need to take out the dashes. Am I incorrect?

            P 1 Reply Last reply
            0
            • J JohnQuar1

              I believe I need to parse it so I can take out the dashes. When I run the code, I get the error "Incorrect syntax near '-'." and it breaks at SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection); So when I read that, that sounds like I need to take out the dashes. Am I incorrect?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6
              1. Put the GUID in apostrophes if you insist on writing poor SQL. 1) Use a parameterized query to write good SQL.
              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