Need to parse GUID
-
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!
-
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!
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. -
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. -
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.`
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.
-
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.
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? -
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?- Put the GUID in apostrophes if you insist on writing poor SQL. 1) Use a parameterized query to write good SQL.