带有命名参数的SQL update语句

时间:2014-03-05 13:38:29

标签: c# sql .net sql-server asp.net-mvc

public void UpdateRegistration(User userModel)
    {
        using (IDbConnection connection = BaseRepository.OpenConnection())
        {
            string query = "UPDATE [dbo].[TBUser] " +
                "(CreatedBy, CreatedDate, ModifiedBy, ModifiedDate) VALUES " +
                "(@CreatedBy, @CreatedDate, @ModifiedBy, @ModifiedDate) WHERE " +
                "UserId = @UserId;";
            connection.Execute(query, userModel);
        }
    }

运行时出现以下异常

<Message>An error has occurred.</Message>
<ExceptionMessage>Incorrect syntax near '('.</ExceptionMessage>
<ExceptionType>System.Data.SqlClient.SqlException</ExceptionType>

userModel具有与之关联的所有属性。

1 个答案:

答案 0 :(得分:2)

语法是

UPDATE [table]
SET [field] = Value
WHERE ....

请参阅http://technet.microsoft.com/en-us/library/ms177523.aspx

所以你的查询应该是

UPDATE [dbo].[TBUser] 
SET CreatedBy = @CreatedBy, 
    CreatedDate = @CreatedDate,
    ModifiedBy = @ModifiedBy,
    ModifiedDate = @ModifiedDate
WHERE UserId = @UserId;