C# 使用 SqlCommand.Parameters 更新表
我正在尝试使用 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)
I'm trying to update an MSSQL table using SqlCommand, I think it's a syntax error with my T-SQL, but here is what I have so far:
SqlCommand sqlCmd = new SqlCommand("UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text = @textParam, statusid = @statusIDParam, ticketClass = @ticketClassParam WHERE id = @ticketIDParam", sqlConn);
The parameters are working as they should, however, the table never gets updated when I run the code. Any help would be appreciated =)
Here is the rest of the code:
#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();
}
And the method's signature:
public static void updateTicketInDatabase(Ticket ticketToBeSubmitted, Label sqlErrorLabel, int ticketID)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
UPDATE FROM 是无效语法(编辑:OP 更正了这一点)。问题也可能出在“
text
”列。text
是 SQL Server 中的关键字,因为它是一种数据类型。尝试用括号括起来。UPDATE FROM is invalid syntax (edit: OP corrected this). The problem might also be the "
text
" column.text
is a keyword in SQL Server, since it's a datatype. Try putting brackets around it.必须使用 if(!Page.IsPostBack)
Had to use if(!Page.IsPostBack)
有几个问题:
Couple of questions:
听起来您的托管提供商限制了您的调试选项,迫使您以老式的方式进行操作。如果在更新之后立即输入类似以下内容会怎么样:
然后执行 ExecuteScalar,而不是执行 ExecuteNonQuery,然后查看 SQL 是否认为其更新了任何内容。
Sounds like your hosting provider limits your debug options, forcing you to do it the old fashioned way. What if immediately after the update, you put something like:
then instead of ExecuteNonQuery, do ExecuteScalar, and see if SQL even thinks its updated anything.
我不知道自从你第一次问以来情况是否有所改变。但这对我有用,例如:
在你的代码中它是:
I don't know if it changed since you first asked. but this works for me , e.g :
In your code it's :