简体   繁体   English

C#SqlCommand查询更新

[英]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 当我从textBox1 = 999添加值时,textBox2 = 500,textBox3 = 50

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

I know SQL query like that. 我知道这样的SQL查询。 But I don't know how to write code in SqlCommand 但是我不知道如何在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 谢谢

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 ): 请不要忘记处理 (显式Close反模式 ):

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("Амжилттай");
  }
}

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

You can use += operator for update. 您可以使用+ =运算符进行更新。 Change your sql command like this; 像这样更改您的sql命令;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM