コード例 #1
0
ファイル: Repository.cs プロジェクト: HrvojeTadin/SQLVehicles
        public void AddVehicleDB(string registration, string name, string color, string typeOfVehicle, string brandOfVehcile, Guid?driverID)
        {
            using SqlConnection openConnection = new SqlConnection(connectionString);

            using SqlCommand command = new SqlCommand("AddVehicle", openConnection);
            command.CommandType      = CommandType.StoredProcedure;

            Guid typeOfVehicleID  = VehicleTypes.Find(x => x.Code == typeOfVehicle).ID;
            Guid brandOfVehicleID = VehicleBrands.Find(x => x.Code == brandOfVehcile).ID;

            command.Parameters.Add("@Registration", SqlDbType.NVarChar, 10).Value             = registration;
            command.Parameters.Add("@Name", SqlDbType.NVarChar, 30).Value                     = name;
            command.Parameters.Add("@Color", SqlDbType.NVarChar, 30).Value                    = color;
            command.Parameters.Add("@TypeOfVehicleID", SqlDbType.UniqueIdentifier, 30).Value  = typeOfVehicleID;
            command.Parameters.Add("@BrandOfVehicleID", SqlDbType.UniqueIdentifier, 30).Value = brandOfVehicleID;
            command.Parameters.Add("@DriverID", SqlDbType.UniqueIdentifier, 30).Value         = driverID;

            openConnection.Open();
            int rowsAffected = command.ExecuteNonQuery();

            if (rowsAffected > 0)
            {
                logger.Debug("The vehicle is inserted in database.");
            }
            else
            {
                logger.Debug("Insert of vehicle did not succeed.");
                throw new Exception();
            }
        }