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

时间:2018-07-17 20:30:52

标签: c# sql-server ado.net

在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";

1 个答案:

答案 0 :(得分:0)

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

如下更新命令文本-

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

添加此行以传递参数

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