当新值是局部变量时,SQL UPDATE的语法是什么



在C#Windows程序中,我想更新SQL Server表中的Int字段

int NewSeqno = MySeqno;    // get current sequence number
NewSeqno++;     // increment it
connection.Open();
// The following errors out saying that NewSeqno is not a column in the table.
// I want to update the field with the local variable NewSeqno.
command.CommandText = "UPDATE dbo.params SET NextSeqno = NewSeqno";
int recordsAffected = command.ExecuteNonQuery();
// The following statement, which writes a constant in the field, works fine.
command.CommandText = "UPDATE dbo.params SET NextSeqno = 123";

您必须将参数传递给SQL查询

更新命令文本如下-

command.CommandText = "UPDATE dbo.params SET NextSeqno = @NewSeqno";

添加此行以传递参数

command.Parameters.Add("@NewSeqno", SqlDbType.Int).Value = NewSeqno;

最新更新