Visual Studio 参考ID SQL/ C#的问题

pieyvz9o  于 2023-11-21  发布在  C#
关注(0)|答案(1)|浏览(147)

我有一个前端应用程序,它与SQL服务器对话,从表中提取信息,并通过每行自动分配的ID查找这些信息。
下面是我目前使用的代码:

  1. String ID = Request.Query["ID"];
  2. try
  3. {
  4. String connectionString = "Data Source=SQLTableDataSource;Integrated Security=True;TrustServerCertificate=True;";
  5. using (SqlConnection connection = new SqlConnection(connectionString))
  6. {
  7. connection.Open();
  8. //String sql = "SELECT TOP (1000) [ID]\r\n ,[tNumber]\r\n ,[tStatus]\r\n ,[tApplication]\r\n ,ISNULL([tType],'') [tType]\r\n ,[tDateLogged]\r\n ,[tDateUpdated]\r\n ,[tDateClosed]\r\n ,[tDateLastChecked]\r\n ,[tUserLogged]\r\n ,ISNULL ([tCurrentlyWith], '') [tCurrentlyWith]\r\n ,[tLoggedOrAdHocTTL]\r\n FROM [JB_DB].[dbo].[Tble_COF_IT_UK_Tickets] WHERE ID=@ID;";
  9. //String sql = "SELECT * FROM Tble_COF_IT_UK_Tickets WHERE ID=@ID;";
  10. String sql = "SELECT TOP (1000) [ID],[tNumber],[tStatus], [tApplication], [tType], [tDateLogged], ISNULL ([tDateUpdated], ' ') [tDateUpdated], ISNULL ([tDateClosed],' ') [tDateClosed], [tDateLastChecked], [tUserLogged], [tCurrentlyWith], [tLoggedOrAdHocTTL] FROM [JB_DB].[dbo].[Tble_COF_IT_UK_Tickets] WHERE ID=@ID;";
  11. using (SqlCommand command = new SqlCommand(sql, connection))
  12. {
  13. command.Parameters.AddWithValue("@ID", ID);
  14. using (SqlDataReader reader = command.ExecuteReader())
  15. {
  16. if (reader.Read())
  17. {
  18. ticketInfo.ID = "" + reader.GetInt32(0);
  19. ticketInfo.tNumber = reader.GetString(1);
  20. ticketInfo.tStatus = reader.GetString(2);
  21. ticketInfo.tApplication = reader.GetString(3);
  22. ticketInfo.tType = reader.GetString(4);
  23. ticketInfo.tDateLogged = reader.GetString(5);
  24. ticketInfo.tDateUpdated = reader.GetString(6);
  25. ticketInfo.tDateClosed = reader.GetString(7);
  26. ticketInfo.tDateLastChecked = reader.GetString(8);
  27. ticketInfo.tUserLogged = reader.GetString(9);
  28. ticketInfo.tCurrentlyWith = reader.GetString(10);
  29. ticketInfo.tLoggedOrAdHocTTL = reader.GetString(11);
  30. }
  31. }
  32. }
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. errorMessage = ex.Message;
  38. }

字符串
然而,当我运行应用程序,我进入门票编辑页面,我收到下面的错误消息:
第一个月
我试图编辑@ID的位置,并改变了ID的大写,但同样的错误发生。
/////////////////////////////////////////////////////////////////
更新。我稍微修改了代码,现在收到一条新的错误消息:
'已声明变量名'@tDateUpdated'。变量名在查询批处理或存储过程中必须唯一。必须声明标量变量“@tNumnber”。'
下面是OnPost方法的代码:

  1. public void OnPost()
  2. {
  3. ticketInfo.ID = Request.Form["ID"];
  4. ticketInfo.tNumber = Request.Form["tNumber"];
  5. ticketInfo.tStatus = Request.Form["tStatus"];
  6. ticketInfo.tApplication = Request.Form["tApplication"];
  7. ticketInfo.tType = Request.Form["tType"];
  8. ticketInfo.tDateLogged = Request.Form["tDateLogged"];
  9. ticketInfo.tDateUpdated = Request.Form["tDateUpdated"];
  10. ticketInfo.tDateClosed = Request.Form["tDateClosed"];
  11. ticketInfo.tDateUpdated = Request.Form["tDateUpdated"];
  12. ticketInfo.tCurrentlyWith = Request.Form["tCurrentlyWith"];
  13. ticketInfo.tLoggedOrAdHocTTL = Request.Form["tLoggedOrAdHocTTL"];
  14. try
  15. {
  16. String connectionString = "Data Source=UK-TF-MUK-SQL01\\DEV;Initial Catalog=JB_DB;Integrated Security=True;TrustServerCertificate=True;";
  17. using (SqlConnection connection = new SqlConnection(connectionString))
  18. {
  19. connection.Open();
  20. String Sql = "UPDATE Tble_COF_IT_UK_Tickets " +
  21. "SET tNumber=@tNumnber, tStatus=@tStatus, tApplication=@tApplication, tType=@tType, tDateLogged=@tDateLogged, tDateUpdated=@tDateUpdated, tDateClosed=@tDateClosed, tDateLastUpdated=@tDateLastUpdated, tCurrentlyWith=@tCurrentlyWith, tLoggedOrAdHocTTL=@tLoggedOrAdHocTTL " +
  22. "WHERE ID=@ID;";
  23. using (SqlCommand command = new SqlCommand(Sql, connection))
  24. {
  25. command.Parameters.AddWithValue("@tNumbar", ticketInfo.tNumber);
  26. command.Parameters.AddWithValue("@tStatus", ticketInfo.tStatus);
  27. command.Parameters.AddWithValue("@tApplication", ticketInfo.tApplication);
  28. command.Parameters.AddWithValue("@tType", ticketInfo.tType);
  29. command.Parameters.AddWithValue("@tDateLogged", ticketInfo.tDateLogged);
  30. command.Parameters.AddWithValue("@tDateUpdated", ticketInfo.tDateUpdated);
  31. command.Parameters.AddWithValue("@tDateClosed", ticketInfo.tDateClosed);
  32. command.Parameters.AddWithValue("@tDateUpdated", ticketInfo.tDateUpdated);
  33. command.Parameters.AddWithValue("@tCurrentlyWith", ticketInfo.tCurrentlyWith);
  34. command.Parameters.AddWithValue("@tLoggedOrAdHocTTL", ticketInfo.tLoggedOrAdHocTTL);
  35. command.Parameters.AddWithValue("@ID", ticketInfo.ID);
  36. command.ExecuteNonQuery();
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. errorMessage = ex.Message;
  43. return;
  44. }

5sxhfpxr

5sxhfpxr1#

假设其中一个带有@ID参数名的SQL语句导致了错误,请确保Request.Query["ID"]不是NULL
避免AddWithValue

相关问题