【问题标题】:What is the syntax for a SQL UPDATE when the new value is a local variable [duplicate]当新值是局部变量时,SQL UPDATE 的语法是什么 [重复]
【发布时间】:2018-07-17 20:30:51
【问题描述】:

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

【问题讨论】:

标签: c# sql-server ado.net


【解决方案1】:

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

更新命令文本如下 -

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

添加此行以传递参数

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

【讨论】:

猜你喜欢
  • 2016-12-07
  • 2021-05-21
  • 2015-06-08
  • 1970-01-01
  • 2018-03-02
  • 2017-03-09
  • 2015-09-02
  • 2013-10-23
  • 2014-11-05
相关资源
最近更新 更多