db.Database.UpdateCredit C# (CSharp) Method

UpdateCredit() public method

public UpdateCredit ( Account acc, int amount ) : int
acc Account
amount int
return int
        public int UpdateCredit(Account acc, int amount)
        {
            MySqlCommand cmd = CreateQuery();
            if (amount > 0)
            {
                cmd.CommandText = "UPDATE stats SET totalCredits = totalCredits + @amount WHERE accId=@accId;";
                cmd.Parameters.AddWithValue("@accId", acc.AccountId);
                cmd.Parameters.AddWithValue("@amount", amount);
                cmd.ExecuteNonQuery();
                cmd = CreateQuery();
            }
            cmd.CommandText = @"UPDATE stats SET credits = credits + (@amount) WHERE accId=@accId;
            SELECT credits FROM stats WHERE accId=@accId;";
            cmd.Parameters.AddWithValue("@accId", acc.AccountId);
            cmd.Parameters.AddWithValue("@amount", amount);
            return (int)cmd.ExecuteScalar();
        }

Usage Example

        protected override void HandleRequest()
        {
            using (Database db = new Database())
            {
                try
                {
                    Account acc = db.Verify(Query["guid"], Query["password"], Program.GameData);

                    string classType = Program.GameData.ObjectTypeToId[ushort.Parse(Query["classType"])];

                    if (CheckAccount(acc, db))
                    {
                        int price = Program.GameData.ObjectDescs[ushort.Parse(Query["classType"])].UnlockCost;
                        if (acc.Credits < price) return;
                        db.UpdateCredit(acc, -price);
                        MySqlCommand cmd = db.CreateQuery();
                        cmd.CommandText =
                            "UPDATE unlockedclasses SET available='unrestricted' WHERE accId=@accId AND class=@class;";
                        cmd.Parameters.AddWithValue("@accId", acc.AccountId);
                        cmd.Parameters.AddWithValue("@class", classType);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception e)
                {
                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                    {
                        wtr.WriteLine("<Error>Invalid classType");
                        wtr.Flush();
                        wtr.WriteLine(e);
                    }
                }
            }
        }
All Usage Examples Of db.Database::UpdateCredit