Ejemplo n.º 1
0
        public static bool UpdateCustomer(Customer oldCustomer, Customer newCustomer)
        {
            // create a connection
            MySqlConnection connection      = MMABooksDB.GetConnection();
            string          updateStatement =
                "UPDATE Customers SET " +
                "Name = @NewName, " +
                "Address = @NewAddress, " +
                "City = @NewCity, " +
                "State = @NewState, " +
                "ZipCode = @NewZipCode " +
                "WHERE CustomerID = @OldCustomerID " +
                "AND Name = @OldName " +
                "AND Address = @OldAddress " +
                "AND City = @OldCity " +
                "AND State = @OldState " +
                "AND ZipCode = @OldZipCode";
            // setup the command object
            MySqlCommand updateCommand =
                new MySqlCommand(updateStatement, connection);

            updateCommand.Parameters.AddWithValue(
                "@newName", newCustomer.Name);
            updateCommand.Parameters.AddWithValue(
                "@newAddress", newCustomer.Address);
            updateCommand.Parameters.AddWithValue(
                "@newCity", newCustomer.City);
            updateCommand.Parameters.AddWithValue(
                "@newState", newCustomer.State);
            updateCommand.Parameters.AddWithValue(
                "@newZipCode", newCustomer.ZipCode);
            updateCommand.Parameters.AddWithValue(
                "@oldCustomerID", oldCustomer.CustomerID);
            updateCommand.Parameters.AddWithValue(
                "@oldName", oldCustomer.Name);
            updateCommand.Parameters.AddWithValue(
                "@oldAddress", oldCustomer.Address);
            updateCommand.Parameters.AddWithValue(
                "@oldCity", oldCustomer.City);
            updateCommand.Parameters.AddWithValue(
                "@oldState", oldCustomer.State);
            updateCommand.Parameters.AddWithValue(
                "@oldZipCode", oldCustomer.ZipCode);
            try
            {
                // open the connection
                connection.Open();
                // execute the command
                int updateCommandReturn = updateCommand.ExecuteNonQuery();
                // if the number of records returned = 1, return true otherwise return false
                if (updateCommandReturn == 1)
                {
                    return(true);
                }
            }
            catch (MySqlException ex)
            {
                // throw the exception
                throw ex;
            }
            finally
            {
                // close the connection
                connection.Close();
            }

            return(false);
        }