Bob
Bob

Reputation: 45

How can I update my database field with a new value plus the original value in database

I am trying to update a field in my database by adding an amount to the value inside that field. My temporary fix is to retrieve the original value from the database with a DataReader but I want to avoid using it. This is my sql statement:

OleDbCommand updateBankBalance = new OleDbCommand("UPDATE Users SET [Bank Balance] = @accountIncome WHERE Username = '" + getUsername() + "'", conn);
updateBankBalance.Parameters.AddWithValue("@accountIncome", rAmount.ToString("0.##") + "VALUE IN DATABASE");
updateBankBalance.ExecuteNonQuery();

Note:

Upvotes: 0

Views: 52

Answers (1)

Anup Sharma
Anup Sharma

Reputation: 2083

Try this query:

UPDATE Users SET [Bank Balance] = @accountIncome + [Bank Balance] WHERE Username = ?

If you are using older version of sql then you may use the Concat function like:

UPDATE Users SET [Bank Balance] = Concat(@accountIncome,[Bank Balance]) WHERE Username = ?

Upvotes: 1

Related Questions