Beispiel #1
0
        public static async Task <VehicleData> CreateVehicle(VehicleData vehicleData)
        {
            string query = $@"INSERT INTO Vehicles (`vehicle_name`, `owner_id`, `position_x`, `position_y`, `position_z`) VALUES ('{vehicleData.VehicleName}', {vehicleData.OwnerId}, {vehicleData.PositionX}, {vehicleData.PositionY}, {vehicleData.PositionZ});";

            using (MySqlCommand command = new MySqlCommand(query, Main.Connection))
            {
                try
                {
                    await command.ExecuteNonQueryAsync();

                    vehicleData.Id = (uint)command.LastInsertedId;
                    return(vehicleData);
                }
                catch (Exception e)
                {
                    Main.Logger.LogError(e.Message);
                }
            }

            return(null);
        }
Beispiel #2
0
        public static async void SaveVehicle(VehicleData vehicleData)
        {
            string query = $@"UPDATE Vehicles SET 
                                `owner_id` = '{vehicleData.OwnerId}', 
                                `position_x` = '{vehicleData.PositionX}', 
                                `position_y` = '{vehicleData.PositionY}', 
                                `position_z` = '{vehicleData.PositionZ}',
                                `heading` = '{vehicleData.Heading}',
                                `color_1` = {vehicleData.Color1},
                                `color_2` = {vehicleData.Color2} WHERE id = {vehicleData.Id}";

            using (MySqlCommand command = new MySqlCommand(query, Main.Connection))
            {
                try
                {
                    await command.ExecuteNonQueryAsync();
                }
                catch (Exception e)
                {
                    Main.Logger.LogError(e.Message);
                }
            }
        }
Beispiel #3
0
        public static async Task <VehicleData> GetVehicleData(uint vehicleId)
        {
            string query = $"SELECT * FROM `vehicles` WHERE `id` = {vehicleId}";

            using (MySqlCommand command = new MySqlCommand(query, Main.Connection))
            {
                try
                {
                    using (DbDataReader reader = await command.ExecuteReaderAsync())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                            VehicleData vehicleData = new VehicleData()
                            {
                                Id          = (uint)reader[0],
                                VehicleName = reader[1].ToString(),
                                Color1      = (int)reader[2],
                                Color2      = (int)reader[3],
                                PositionX   = reader.FloatOrNull(4),
                                PositionY   = reader.FloatOrNull(5),
                                PositionZ   = reader.FloatOrNull(6),
                                Heading     = reader.FloatOrNull(7),
                                OwnerId     = uint.Parse(reader[8].ToString())
                            };
                            return(vehicleData);
                        }
                    }
                }
                catch (Exception e)
                {
                    Main.Logger.LogError(e.Message);
                }
            }

            return(null);
        }