Tamiraa Aquarius
Tamiraa Aquarius

Reputation: 47

C# SqlCommand query with update

I tried to find it. But I can't found exactly my answer. So I decide to ask this questions. I need your help.

I want add value into table value without overwriting Debit, Score column. It will add current value.

cmd = new SqlCommand("UPDATE Users SET Debit=@debit, 
                                       Score=@score 
                                 WHERE Phone=@phone", con);

con.Open();

cmd.Parameters.AddWithValue("@phone", textBox1.Text);
cmd.Parameters.AddWithValue("@debit", textBox2.Text);
cmd.Parameters.AddWithValue("@score", textBox3.Text);

cmd.ExecuteNonQuery();

MessageBox.Show("Амжилттай");
con.Close();

For example:

Table, Phone: 999 | Debit: 1500 | Score: 100 //current <br>

When I add value from textBox1 = 999, textBox2 = 500, textBox3 = 50

Table, Phone: 999, Debit: 2000, Score: 150 //updating like that 

I know SQL query like that. But I don't know how to write code in SqlCommand

UPDATE Users 
SET Debit = Debit + [user input], Score = Score + [user input] 
WHERE = Phone

Any suggestions?

(Sorry for my horrible English I hope you guys understand What I'm trying to ask)

Thanks

Upvotes: 2

Views: 5084

Answers (3)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

If you want to add, just add:

cmd = new SqlCommand(@"UPDATE Users 
                          SET Debit = Debit + @debit, 
                              Score = Score + @score 
                        WHERE Phone = @phone", con);

Please, notice verbatim string @"..." syntax. Please, do not forget about disposing (explicit Close is an antipattern):

string sql = 
  @"UPDATE Users 
       SET Debit = Debit + @debit, 
           Score = Score + @score 
     WHERE Phone = @phone";

//TODO: put the right connection string instead of "MyConnectionStringHere"
//DONE: IDisposable (SqlConnection) should be wrapped into using 
using (var con = new SqlConnection("MyConnectionStringHere")) {
  con.Open();

  //DONE: IDisposable (SqlCommand) should be wrapped into using
  using (var cmd = new SqlCommand(sql, con)) {
    //TODO: AddWithValue is often a bad choice; change to Add 
    cmd.Parameters.AddWithValue("@phone", textBox1.Text);
    cmd.Parameters.AddWithValue("@debit", textBox2.Text);
    cmd.Parameters.AddWithValue("@score", textBox3.Text);

    cmd.ExecuteNonQuery();
    //TODO: a better policy is to read localized strings from resources
    MessageBox.Show("Амжилттай");
  }
}

Upvotes: 6

Saalim Bhoraniya
Saalim Bhoraniya

Reputation: 360

This will help you....just try in this way..

SqlCommand cmd = new SqlCommand("UPDATE Users SET Debit = Debit + " + textBox2.Text + ", Score = Score + " + textBox3.Text + " WHERE Phone = " + textBox1.Text + "", con);
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Амжилттай");
                con.Close();

OR

SqlCommand cmd = new SqlCommand("UPDATE Users SET Debit = Debit + @debit, Score = Score + @score WHERE Phone = @phone", con);
                con.Open();
                cmd.Parameters.AddWithValue("@phone", textBox1.Text);
                cmd.Parameters.AddWithValue("@debit", textBox2.Text);
                cmd.Parameters.AddWithValue("@score", textBox3.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Амжилттай");
                con.Close();

Upvotes: 1

Abdulkadir Erkmen
Abdulkadir Erkmen

Reputation: 202

You can use += operator for update. Change your sql command like this;

UPDATE Users SET Debit+=@debit, 
                 Score+=@score 
                                 WHERE Phone=@phone

Upvotes: 0

Related Questions