Ismael Oliveira

Ismael Oliveira

  • 1.5k
  • 115
  • 7.8k

Insert of a record in a DB with error

Nov 4 2020 2:54 PM
Hi, folks.
 
I’m creating an application in C# with Visual Studio which works with SQLSERVER. I created a routine to write data in the DB, which inserts data in 2 tables. In the first one it’s working fine, but the second always gives an error with the message: “code 102: incorrect syntax next to ‘(‘”.
 
I’ve executed step by step and the syntax seems to be correct. I changed the way the command is composed, but the error persists. The change is a commented piece of code The code of the routine is as follows.
  1. public static bool GravaRegistroDuplo(string StabelaPri, List<string> ScamposPri, List<string> StiposPri, List<string> SdadosPri, string StabelaSec,  
  2. List<string> ScamposSec, List<string> StiposSec, List<string> SdadosSec, ref string Smensagem)  
  3. {  
  4. string TextoSQL = "insert into " + StabelaPri + "(";  
  5. int i;  
  6. // Aqui são adicionados os campos da tabela principal no comando INSERT  
  7. for (i = 0; i < ScamposPri.Count; i++)  
  8. TextoSQL += ScamposPri[i] + ",";  
  9. TextoSQL = TextoSQL.Substring(0, TextoSQL.Length - 1) + ")";  
  10. // Aqui são acrescentados os parâmetros do comando  
  11. TextoSQL += " values(";  
  12. for (i = 0; i < SdadosPri.Count; i++)  
  13. TextoSQL += "@Val" + i + ",";  
  14. TextoSQL = TextoSQL.Substring(0, TextoSQL.Length - 1) + ")";  
  15. using (var con = new SqlConnection(ConfigurationManager.AppSettings["TextoConexao"]))  
  16. {  
  17. con.Open();  
  18. // É iniciada uma transação que sofrerá o COMMIT (ou ROLLBACK)  
  19. SqlTransaction Tran = con.BeginTransaction();  
  20. try  
  21. {  
  22. string nome;  
  23. SqlCommand Cmd = new SqlCommand(TextoSQL, con, Tran);  
  24. // Aqui são acrescentados os valores a serem inseridos na tabela principal por meio dos parâmetros  
  25. for (i = 0; i < SdadosPri.Count; i++)  
  26. {  
  27. nome = "@Val" + i.ToString();  
  28. if (StiposPri[i] == "numeric")  
  29. Cmd.Parameters.AddWithValue(nome, float.Parse(SdadosPri[i]));  
  30. else  
  31. Cmd.Parameters.AddWithValue(nome, SdadosPri[i]);  
  32. }  
  33. TextoSQL = TextoSQL.Substring(0, TextoSQL.Length - 1) + ")";  
  34. Cmd.ExecuteNonQuery();  
  35. // Definição do texto SQL para obtenção do código do registro inserido  
  36. Cmd.Dispose();  
  37. TextoSQL = "select @@identity";  
  38. Cmd = new SqlCommand(TextoSQL, con, Tran);  
  39. SqlDataReader rdr;  
  40. rdr = Cmd.ExecuteReader();  
  41. // Cod1 é o código do registro recém-inserido na tabela  
  42. if (rdr.Read())  
  43. {  
  44. if (rdr.GetValue(0).ToString() != "")  
  45. SdadosSec[0] = rdr.GetValue(0).ToString();  
  46. }  
  47. rdr.Close();  
  48. // Agora será introduzido o registro na tabela secundária  
  49. Cmd.Dispose();  
  50. TextoSQL = "insert into " + StabelaSec + "(";  
  51. Cmd = new SqlCommand(TextoSQL, con, Tran);  
  52. for (i = 0; i < ScamposSec.Count; i++)  
  53. TextoSQL += ScamposSec[i] + ",";  
  54. TextoSQL = TextoSQL.Substring(0, TextoSQL.Length - 1) + ")";  
  55. // Aqui são acrescentados os parâmetros do comando na tabela secundária  
  56. TextoSQL += " values(";  
  57. for (i = 0; i < ScamposSec.Count; i++)  
  58. {  
  59. if (StiposSec[i] == "integer")  
  60. TextoSQL += SdadosSec[i] + ",";  
  61. else if (StiposSec[i] == "numeric")  
  62. TextoSQL += SdadosSec[i] + ",";  
  63. else if (StiposSec[i] == "string")  
  64. TextoSQL += "'" + SdadosSec[i] + "',";  
  65. else if (StiposSec[i] == "date")  
  66. TextoSQL += "'" + SdadosSec[i] + "',";  
  67. }  
  68. /* 
  69. for (i = 0; i < ScamposSec.Count; i++) 
  70. { 
  71. nome = "@Val" + i.ToString(); 
  72. TextoSQL += nome + ","; 
  73. if (StiposSec[i] == "numeric") 
  74. Cmd.Parameters.AddWithValue(nome, float.Parse(SdadosSec[i])); 
  75. else if (StiposSec[i] == "string") 
  76. Cmd.Parameters.AddWithValue(nome, SdadosSec[i]); 
  77. else if (StiposSec[i] == "integer") 
  78. Cmd.Parameters.AddWithValue(nome, int.Parse(SdadosSec[i])); 
  79. else if (StiposSec[i] == "date") 
  80. Cmd.Parameters.AddWithValue(nome, DateTime.Parse(SdadosSec[i])); 
  81. } 
  82. */  
  83. TextoSQL = TextoSQL.Substring(0, TextoSQL.Length - 1) + ")";  
  84. Cmd.ExecuteNonQuery();  
  85. // Validação das inserções no BD  
  86. Tran.Commit();  
  87. Smensagem = "Sucesso";  
  88. return true;  
  89. }  
  90. catch (SqlException ex)  
  91. {  
  92. Tran.Rollback();  
  93. Smensagem = "Código: " + ex.Number + "\nMensagem: " + ex.Message;  
  94. return false;  
  95. }  
  96. }  
  97. }  
Can anybody help me? Thanks a lot.

Answers (2)