如果DB值等于100,则增加DB值

时间:2014-05-15 09:05:13

标签: c# asp.net

得到一个名为Player的数据库表,得到的值是Level SMALLINT和Experience SMALLINT + others。如果经验等于50或更高,我想将等级提高到2;我已尝试过这个以及其他一些事情:

    SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    SqlCommand CheckStat = new SqlCommand("SELECT * FROM Player WHERE UserID=@uid", connection);
    string uID = Session["userID"].ToString();
    CheckStat.Parameters.AddWithValue("@uid", uID);

    try
    {

        connection.Open();
        SqlDataReader StatReader = null;
        StatReader = CheckStat.ExecuteReader();


        if (StatReader.Read())
        {
            ExperienceLabel.Text = "Experience: " + StatReader["Experience"].ToString();
            EnergyLabel.Text = "Energy: " + StatReader["Energy"].ToString();
            GoldLabel.Text = "Gold: " + StatReader["Gold"].ToString();
            int Experience = Convert.ToInt32(StatReader["Experience"]);
            if (Experience >= 50)
            {
                SqlCommand Level2 = new SqlCommand("UPDATE Player SET player.Level=2 WHERE UserID=@uid", connection);
                try
                {
                    connection.Open();
                    Level2.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    // Error handling
                    LevelLabel.Text = (ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }


        }

    }
    catch (Exception ex)
    {
        ExperienceLabel.Text = (ex.Message);

    }

1 个答案:

答案 0 :(得分:0)

您似乎没有向SqlCommand Level2提供@uid参数。