【问题标题】:C# SqlCommand query with update带有更新的 C# SqlCommand 查询
【发布时间】:2017-10-17 06:39:40
【问题描述】:

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

我想在不覆盖借记、分数列的情况下将值添加到表值中。它将添加当前值。

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

有什么建议吗?

(对不起我糟糕的英语,希望你们能理解我想问的问题)

谢谢

【问题讨论】:

  • 你的SqlCommand好像没问题,是什么问题?

标签: c# sql-server sqlcommand


【解决方案1】:

如果你想添加,只需添加

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

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

【讨论】:

  • 它解决了我的问题。谢谢!我非常感谢。 :)
【解决方案2】:

这会对你有所帮助....就这样试试吧..

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

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

【讨论】:

  • 请不要hardcode sql,而是使用parameters(如问题中所示)
  • 但输出将与 Dmitry Bychenko 相同。
  • 是的,输出将是相同的,但是: 1.(几乎)所有查询都会不同(因此优化器应该必须为每个查询生成计划); 2. 查询容易出现sql 注入:假设textBox2.Text 包含123; --。在这种情况下,您将查询:"UPDATE Users SET Debit = Debit + 123 -- ..." (please, notice **commenting** --). So input 123; --` 将更新整个表,而不仅仅是所需的电话
  • ohhh..是的,非常感谢 Dmitry Bychenko...我的查询不安全。
  • 我试过这种方式,但它并没有解决我的问题。我更喜欢使用参数
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    相关资源
    最近更新 更多