c# Sql Server更新了多个where子句和多个id字段

本文关键字:子句 id 字段 where Sql 更新 Server | 更新日期: 2023-09-27 18:01:54

我在字符串"之后得到未闭引号。我已经尝试了所有的方法,如果你能帮助我,我将不胜感激。

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sipConnectionString"].ConnectionString);
protected void Button1_Click(object sender, EventArgs e)
{
    conn.Open();
    string query = "select dealercode, dropdate, couponno from coupon where dealercode = '" + DEALERCODETextBox.Text + "' and dropdate = '" + DROPDATETextBox.Text + "' and COUPONNO = '" + COUPONCOUNTTextBox.Text +"','";
    SqlCommand cm = new SqlCommand(query, conn);
    cm.Parameters.AddWithValue("@couponcount", COUPONCOUNTTextBox.Text);
    cm.Parameters.AddWithValue("@totalrev", GRANDTOTALTextBox.Text);
    cm.ExecuteNonQuery();
    conn.Close();

c# Sql Server更新了多个where子句和多个id字段

在查询字符串的最后一个

and COUPONNO = '" + COUPONCOUNTTextBox.Text +"','";

"'";代替+"','";

注意:您的查询字符串也缺少Parameters

可以使用参数来添加值,但是在查询中不使用参数:

    string query = "select dealercode, dropdate, couponno from coupon where dealercode = @dealercode and dropdate =@dropdate and COUPONNO = @couponcount;";
SqlCommand cm = new SqlCommand(query, conn);
cm.Parameters.AddWithValue("@couponcount", COUPONCOUNTTextBox.Text);
cm.Parameters.AddWithValue("@dealercode ", DEALERCODETextBox.Text);
cm.Parameters.AddWithValue("@dropdate ", DROPDATETextBox.Text);

换行:

string query = "select dealercode, dropdate, couponno 
   from coupon where dealercode = '" + DEALERCODETextBox.Text + "' 
   and dropdate = '" + DROPDATETextBox.Text + "' 
   and COUPONNO = '" + COUPONCOUNTTextBox.Text +"'";
SqlCommand cm = new SqlCommand(query, conn);