Collins
Collins

Reputation: 231

Sql update statement with named parameters

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);
        }
    }

I get the following exception while it runs

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

userModel has all of the properties associated with it.

Upvotes: 1

Views: 466

Answers (1)

podiluska
podiluska

Reputation: 51504

The syntax is

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

See http://technet.microsoft.com/en-us/library/ms177523.aspx

So your query should be

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

Upvotes: 3

Related Questions