C# 当新值是局部变量时,SQL更新的语法是什么
在C#Windows程序中,我想更新SQL Server表中的Int字段C# 当新值是局部变量时,SQL更新的语法是什么,c#,sql-server,ado.net,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 t
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";
将此行添加到pass参数
command.Parameters.Add("@NewSeqno", SqlDbType.Int).Value = NewSeqno;
你应该学会如何去做和做。例如,指向MSDN的链接有一个更新声明。同意,更新了我的答案。