コード例 #1
0
 /// <summary>
 /// Constructor to initialize values from the database
 /// </summary>
 /// <param name="id">Primary Key</param>
 public Vehicle(int id)
 {
     dbCommand             = new SqlCommand("GetVehicle", dbConnection);
     dbCommand.CommandType = System.Data.CommandType.StoredProcedure;
     dbCommand.Parameters.Add(new SqlParameter("@vId", System.Data.SqlDbType.Int)).Value = id;
     dbConnection.Open();
     try
     {
         using (dbReader = dbCommand.ExecuteReader())
         {
             while (dbReader.Read())
             {
                 this.id             = (int)dbReader[0];
                 registerationNumber = new RegisterationNumberFormat((string)dbReader["RAlphabets"], (short)dbReader["RNumber"], (short)dbReader["RYear"]);
                 model        = (string)dbReader[4];
                 vehicleColor = (Colors)dbReader[5];
                 engineCC     = (short)dbReader[6];
                 isAc         = (bool)dbReader[7];
                 driver       = new Driver((long)dbReader[8]);
                 type         = new VehicleType((int)dbReader[9]);
             }
         }
     }
     catch (SqlException ex)
     {
         dbConnection.Close();
         throw new DbQueryProcessingFailedException("Vehicle->Consturctor(int)", ex);
     }
     dbConnection.Close();
 }
コード例 #2
0
 /// <summary>
 /// Constructor to add a new vehicle
 /// </summary>
 /// <param name="registerationNumber">Registeration Number of the vehicle provided by the government</param>
 /// <param name="model">model or make of the vehicle</param>
 /// <param name="engineCC">Engine of the vehicle</param>
 /// <param name="isAc">Whether if the vehicle is air conditioned</param>
 /// <param name="color">Color of the Vehicle</param>
 /// <param name="type">Type of the vehicle</param>
 /// <param name="driver">Driver who owns the vehicle</param>
 public Vehicle(RegisterationNumberFormat registerationNumber, string model, int engineCC, bool isAc, Colors color, VehicleType type, Driver driver)
 {
     dbCommand             = new SqlCommand("AddNewVehicle", dbConnection);
     dbCommand.CommandType = System.Data.CommandType.StoredProcedure;
     dbCommand.Parameters.Add(new SqlParameter("@uId", System.Data.SqlDbType.BigInt)).Value      = driver.UserId;
     dbCommand.Parameters.Add(new SqlParameter("@rNum", System.Data.SqlDbType.SmallInt)).Value   = registerationNumber.Number;
     dbCommand.Parameters.Add(new SqlParameter("@rAlpha", System.Data.SqlDbType.VarChar)).Value  = registerationNumber.Alphabets;
     dbCommand.Parameters.Add(new SqlParameter("@rYear", System.Data.SqlDbType.SmallInt)).Value  = registerationNumber.Year;
     dbCommand.Parameters.Add(new SqlParameter("@model", System.Data.SqlDbType.VarChar)).Value   = model;
     dbCommand.Parameters.Add(new SqlParameter("@color", System.Data.SqlDbType.Int)).Value       = color;
     dbCommand.Parameters.Add(new SqlParameter("@engine", System.Data.SqlDbType.SmallInt)).Value = engineCC;
     dbCommand.Parameters.Add(new SqlParameter("@isAc", System.Data.SqlDbType.Bit)).Value        = isAc;
     dbCommand.Parameters.Add(new SqlParameter("@tId", System.Data.SqlDbType.Int)).Value         = type.TypeId;
     dbConnection.Open();
     try
     {
         id = Convert.ToInt32(dbCommand.ExecuteScalar());
     }
     catch (SqlException ex)
     {
         dbConnection.Close();
         if (ex.Number == 2601 || ex.Number == 2627)
         {
             //Unique key handler
             throw new UniqueKeyViolationException("Cannot add vehicle, because this vehicle is already present for another driver");
         }
         throw new DbQueryProcessingFailedException("Vehicle->Constructor(RegisterationNumberFormat, string, int, bool, Colors, VehicleType, Driver)", ex);
     }
     dbConnection.Close();
     this.registerationNumber = registerationNumber;
     this.engineCC            = (short)engineCC;
     this.model   = model;
     this.isAc    = isAc;
     vehicleColor = color;
     this.driver  = driver;
     this.type    = type;
 }