コード例 #1
0
ファイル: VehicleService.cs プロジェクト: aoeaman/WebApi
        public Vehicle Add(Vehicle vehicle)
        {
            VehicleDBO _vehicle = _mapper.Map <VehicleDBO>(vehicle);

            try
            {
                _vehicle.IsActive = true;
                _context.Vehicles.Add(_vehicle);
                _context.SaveChanges();
                return(_mapper.Map <Vehicle>(_vehicle));
            }
            catch (Exception)
            {
                _context.Vehicles.Remove(_vehicle);
                return(null);
            }
        }
コード例 #2
0
ファイル: DBL.cs プロジェクト: aaronrc79/AaronChambersPLAR
        /// <summary>
        /// Gets the list of vehicles from the database and returns it as a VehicleDBO object
        /// </summary>
        /// <returns>List of VehicleFBO objects</returns>
        internal static List <VehicleDBO> GetVehicleList()
        {
            // Declare the SQL connection, SQL command, and SQL adapter
            SqlConnection dbConnection = new SqlConnection(GetConnectionString());
            SqlCommand    command      = new SqlCommand("SELECT * FROM [tblVehicles]", dbConnection);

            //Declare an empty list of VehicleDBO objects
            List <VehicleDBO> allVehicles = new List <VehicleDBO>();

            //Used ID in the table
            var count = 1;

            // Try to connect to the database, and use the adapter to fill the table
            try
            {
                //Open the connection
                dbConnection.Open();

                //Use reader as the sql excecute reader command
                using (var reader = command.ExecuteReader())
                {
                    //While there are still rows to be read
                    while (reader.Read())
                    {
                        //Create a new VehicleDBO object
                        var tVehicle = new VehicleDBO();
                        //Set the id to the count
                        tVehicle.id = count.ToString();
                        //Assign the variables
                        tVehicle.manufacturer = (string)reader.GetValue(1);
                        tVehicle.model        = (string)reader.GetValue(2);
                        tVehicle.productYear  = (string)reader.GetValue(3);
                        try
                        {
                            //Try to see if theres a value in the mileage colum of the current row
                            tVehicle.mileage = (string)reader.GetValue(4);
                        }
                        catch
                        {
                            //If there's no mileage it's a boat and set it to null
                            tVehicle.mileage = null;
                        }

                        tVehicle.isCar = (bool)reader.GetValue(5);
                        //Add the vehicle to our list
                        allVehicles.Add(tVehicle);
                        //Update the count
                        count = count + 1;
                    }
                }
            }
            catch (Exception ex)
            {
                //System.Windows.MessageBox.Show("A database error has been encountered: " + Environment.NewLine + ex.Message, "Database Error");
            }
            finally
            {
                //Close the conneciton
                dbConnection.Close();
            }

            // Return the populated DataTable
            return(allVehicles);
        }