带更新的 C# SqlCommand 查询
C# SqlCommand query with update
我试图找到它。但我找不到确切的答案。所以我决定问这个问题。我需要你的帮助。
我想将值添加到 table 值而不覆盖借方、得分列。它将添加当前值。
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
有什么建议吗?
(对不起,我的英语很糟糕,希望你们能理解我想问的问题)
谢谢
如果你想添加,只需添加:
cmd = new SqlCommand(@"UPDATE Users
SET Debit = Debit + @debit,
Score = Score + @score
WHERE Phone = @phone", con);
请注意 verbatim string @"..."
语法。请不要忘记 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("Амжилттай");
}
}
您可以使用 += 运算符进行更新。像这样更改您的 sql 命令;
UPDATE Users SET Debit+=@debit,
Score+=@score
WHERE Phone=@phone
这对你有帮助....就这样试试吧..
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();
我试图找到它。但我找不到确切的答案。所以我决定问这个问题。我需要你的帮助。
我想将值添加到 table 值而不覆盖借方、得分列。它将添加当前值。
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
有什么建议吗?
(对不起,我的英语很糟糕,希望你们能理解我想问的问题)
谢谢
如果你想添加,只需添加:
cmd = new SqlCommand(@"UPDATE Users
SET Debit = Debit + @debit,
Score = Score + @score
WHERE Phone = @phone", con);
请注意 verbatim string @"..."
语法。请不要忘记 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("Амжилттай");
}
}
您可以使用 += 运算符进行更新。像这样更改您的 sql 命令;
UPDATE Users SET Debit+=@debit,
Score+=@score
WHERE Phone=@phone
这对你有帮助....就这样试试吧..
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();