更新查询不能在c#中工作

本文关键字:工作 查询 不能 更新 | 更新日期: 2023-09-27 17:55:05

protected void btn_redeem_Click(object sender, EventArgs e)
{
    int lol = int.Parse(lbl_TotalPrice.Text,System.Globalization.NumberStyles.Currency);
    double nprice = lol * 0.05;
    int newpoints=0 ;
    if (int.Parse(Session["points"].ToString()) >= 1000)
    {
        double redeem = lol - nprice;
        lbl_TotalPrice.Text = redeem.ToString("C");
         newpoints = int.Parse(Session["points"].ToString()) - 1000;
    }
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthDBContext"].ConnectionString);
    conn.Open();
    string queryStr = "UPDATE Users SET Points ='" + newpoints + "'WHERE UserName=" + Session["New"].ToString();
    SqlCommand com = new SqlCommand(queryStr, conn);
    conn.Close();
}

更新查询不能在c#中工作

添加.ExecuteNonQuery来执行查询,并添加try-catch块来捕获任何异常:

try
{
    ...
    SqlCommand com = new SqlCommand(queryStr, conn);
    com.ExecuteNonQuery();
    conn.Close();
    ...
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}