我正在尝试使用SqlCommand更新MSSQL表,我认为这是我的T-SQL的语法错误,但这是我到目前为止:SqlCommand sqlCmd = new SqlCommand(UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text = @t...
我正在尝试使用SqlCommand更新MSSQL表,我认为这是我的T-SQL的语法错误,但这是我到目前为止:
SqlCommand sqlCmd = new SqlCommand("UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text = @textParam, statusid = @statusIDParam, ticketClass = @ticketClassParam WHERE id = @ticketIDParam", sqlConn);
参数正在按预期工作,但是,当我运行代码时,表永远不会更新.任何帮助将不胜感激=)
这是代码的其余部分:
#region Parameters
/* Parameters */
sqlCmd.Parameters.Add("@ticketIDParam", SqlDbType.BigInt);
sqlCmd.Parameters["@ticketIDParam"].Value = ticketID;
sqlCmd.Parameters.Add("@emailParam", SqlDbType.NVarChar);
sqlCmd.Parameters["@emailParam"].Value = ticketToBeSubmitted.getEmail();
sqlCmd.Parameters.Add("@subjectParam", SqlDbType.NVarChar);
sqlCmd.Parameters["@subjectParam"].Value = ticketToBeSubmitted.getSubject();
sqlCmd.Parameters.Add("@textParam", SqlDbType.Text);
sqlCmd.Parameters["@textParam"].Value = ticketToBeSubmitted.getTicketContent();
sqlCmd.Parameters.Add("@statusIDParam", SqlDbType.NVarChar);
sqlCmd.Parameters["@statusIDParam"].Value = ticketToBeSubmitted.getStatus();
sqlCmd.Parameters.Add("@ticketClassParam", SqlDbType.NVarChar);
sqlCmd.Parameters["@ticketClassParam"].Value = ticketToBeSubmitted.getTicketClass();
#endregion
#region Try/Catch/Finally
/* Try/Catch/Finally */
try
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
sqlErrorLabel.Text = sqlEx.ToString();
sqlErrorLabel.ForeColor = System.Drawing.Color.Red;
}
finally
{
sqlConn.Close();
}
并且该方法的签名:
public static void updateTicketInDatabase(Ticket ticketToBeSubmitted, Label sqlErrorLabel, int ticketID)
解决方法:
UPDATE FROM是无效语法(编辑:OP更正此).问题也可能是“文本”列. text是SQL Server中的关键字,因为它是一种数据类型.尝试在它周围放置括号.
UPDATE yak_tickets
SET email = @emailParam,
subject = @subjectParam,
[text] = @textParam,
statusid = @statusIDParam,
ticketClass = @ticketClassParam
WHERE id = @ticketIDParam
沃梦达教程
本文标题为:C#使用SqlCommand.Parameters更新表
基础教程推荐
猜你喜欢
- Unity利用UGUI制作提示框效果 2023-03-03
- C#中实现插入、删除Excel分页符的方法 2022-12-11
- C#自定义鼠标拖拽Drag&Drop效果之基本原理及基本实现代码 2023-05-30
- unity实现简单贪吃蛇游戏 2023-02-16
- C#类型转换之自定义隐式转换和显式转换 2023-05-26
- C# WPF实现的语音播放自定义控件 2023-04-10
- C#使用dynamic一行代码实现反射操作 2023-07-19
- C#中把Json数据转为DataTable 2023-06-04
- .net中常用的正则表达式 2023-06-04
- C# WPF ListView控件的实例详解 2022-11-15