C#SqlCommand查询更新

时间:2017-10-17 06:39:40

标签: c# sql-server sqlcommand

我试图找到它。但我找不到我的答案。所以我决定问这个问题。我需要你的帮助。

我希望在不覆盖Debit,Score列的情况下将值添加到表值中。它会增加当前值。

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

例如:

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

当我从textBox1 = 999添加值时,textBox2 = 500,textBox3 = 50

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

我知道这样的SQL查询。但我不知道如何在SqlCommand

中编写代码
UPDATE Users 
SET Debit = Debit + [user input], Score = Score + [user input] 
WHERE = Phone

有什么建议吗?

(抱歉我的英语很糟糕,我希望你们能理解我想要问的内容)

由于

3 个答案:

答案 0 :(得分:5)

如果您想添加,只需添加

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

请注意 verbatim 字符串@"..."语法。请不要忘记处理(显式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("Амжилттай");
  }
}

答案 1 :(得分:1)

这会帮助你......只是试试这个......

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

答案 2 :(得分:0)

您可以使用+ =运算符进行更新。像这样更改你的sql命令;

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