我有一个前端应用程序,它与SQL服务器对话,从表中提取信息,并通过每行自动分配的ID查找这些信息。
下面是我目前使用的代码:
String ID = Request.Query["ID"];
try
{
String connectionString = "Data Source=SQLTableDataSource;Integrated Security=True;TrustServerCertificate=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//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;";
//String sql = "SELECT * FROM Tble_COF_IT_UK_Tickets WHERE ID=@ID;";
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;";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@ID", ID);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
ticketInfo.ID = "" + reader.GetInt32(0);
ticketInfo.tNumber = reader.GetString(1);
ticketInfo.tStatus = reader.GetString(2);
ticketInfo.tApplication = reader.GetString(3);
ticketInfo.tType = reader.GetString(4);
ticketInfo.tDateLogged = reader.GetString(5);
ticketInfo.tDateUpdated = reader.GetString(6);
ticketInfo.tDateClosed = reader.GetString(7);
ticketInfo.tDateLastChecked = reader.GetString(8);
ticketInfo.tUserLogged = reader.GetString(9);
ticketInfo.tCurrentlyWith = reader.GetString(10);
ticketInfo.tLoggedOrAdHocTTL = reader.GetString(11);
}
}
}
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
字符串
然而,当我运行应用程序,我进入门票编辑页面,我收到下面的错误消息:
第一个月
我试图编辑@ID的位置,并改变了ID的大写,但同样的错误发生。
/////////////////////////////////////////////////////////////////
更新。我稍微修改了代码,现在收到一条新的错误消息:
'已声明变量名'@tDateUpdated'。变量名在查询批处理或存储过程中必须唯一。必须声明标量变量“@tNumnber”。'
下面是OnPost方法的代码:
public void OnPost()
{
ticketInfo.ID = Request.Form["ID"];
ticketInfo.tNumber = Request.Form["tNumber"];
ticketInfo.tStatus = Request.Form["tStatus"];
ticketInfo.tApplication = Request.Form["tApplication"];
ticketInfo.tType = Request.Form["tType"];
ticketInfo.tDateLogged = Request.Form["tDateLogged"];
ticketInfo.tDateUpdated = Request.Form["tDateUpdated"];
ticketInfo.tDateClosed = Request.Form["tDateClosed"];
ticketInfo.tDateUpdated = Request.Form["tDateUpdated"];
ticketInfo.tCurrentlyWith = Request.Form["tCurrentlyWith"];
ticketInfo.tLoggedOrAdHocTTL = Request.Form["tLoggedOrAdHocTTL"];
try
{
String connectionString = "Data Source=UK-TF-MUK-SQL01\\DEV;Initial Catalog=JB_DB;Integrated Security=True;TrustServerCertificate=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String Sql = "UPDATE Tble_COF_IT_UK_Tickets " +
"SET tNumber=@tNumnber, tStatus=@tStatus, tApplication=@tApplication, tType=@tType, tDateLogged=@tDateLogged, tDateUpdated=@tDateUpdated, tDateClosed=@tDateClosed, tDateLastUpdated=@tDateLastUpdated, tCurrentlyWith=@tCurrentlyWith, tLoggedOrAdHocTTL=@tLoggedOrAdHocTTL " +
"WHERE ID=@ID;";
using (SqlCommand command = new SqlCommand(Sql, connection))
{
command.Parameters.AddWithValue("@tNumbar", ticketInfo.tNumber);
command.Parameters.AddWithValue("@tStatus", ticketInfo.tStatus);
command.Parameters.AddWithValue("@tApplication", ticketInfo.tApplication);
command.Parameters.AddWithValue("@tType", ticketInfo.tType);
command.Parameters.AddWithValue("@tDateLogged", ticketInfo.tDateLogged);
command.Parameters.AddWithValue("@tDateUpdated", ticketInfo.tDateUpdated);
command.Parameters.AddWithValue("@tDateClosed", ticketInfo.tDateClosed);
command.Parameters.AddWithValue("@tDateUpdated", ticketInfo.tDateUpdated);
command.Parameters.AddWithValue("@tCurrentlyWith", ticketInfo.tCurrentlyWith);
command.Parameters.AddWithValue("@tLoggedOrAdHocTTL", ticketInfo.tLoggedOrAdHocTTL);
command.Parameters.AddWithValue("@ID", ticketInfo.ID);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
return;
}
型
1条答案
按热度按时间5sxhfpxr1#
假设其中一个带有
@ID
参数名的SQL语句导致了错误,请确保Request.Query["ID"]
不是NULL
。避免AddWithValue。