コード例 #1
0
ファイル: EmployeeDatabase.cs プロジェクト: war-man/Scuola
        public List <Employee> FindAll()
        {
            _connection = new SqlConnection(_connectionString);

            _connection.Open();

            var cmd = _connection.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Employees";

            var reader = cmd.ExecuteReader();

            var list = new List <Employee>();

            while (reader.Read())
            {
                var e = new Employee();

                e.Id              = (int)reader["Id"];
                e.Name            = (string)reader["Name"];
                e.TotalPay        = (decimal)reader["TotalPay"];
                e.BonusCalculator =
                    createBonusCalculator((string)reader["BonusCalculatorType"]);

                var payType = (string)reader["PayCalculatorType"];

                switch (payType)
                {
                case nameof(FixedPayCalculator):
                    var fixedCalc = new FixedPayCalculator();
                    fixedCalc.MonthlySalary = (decimal)reader["MonthlySalary"];
                    break;

                case nameof(HourlyPayCalculator):
                    var hourlyCalc = new HourlyPayCalculator();
                    hourlyCalc.HourlySalary = (decimal)reader["HourlySalary"];
                    // TODO: pesca le schedulations di questo employee.
                    break;

                case nameof(CommissionPayCalculator):
                    var commCalc = new CommissionPayCalculator();
                    commCalc.CommissionPercentage = (decimal)reader["CommissionPercentage"];
                    //TODO: pesca le Commissions di questo Employee
                    break;

                case nameof(NullPayCalculator):
                    break;

                default:
                    throw new ArgumentException($"'{payType}' is not a known PayCalculator!");
                }

                list.Add(e);
            }

            _connection.Close();

            return(list);
        }
コード例 #2
0
ファイル: EmployeeDatabase.cs プロジェクト: war-man/Scuola
        public void Save(Employee e, FixedPayCalculator c)
        {
            var cmd = _connection.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText =
                " UPDATE Employees SET" +
                " PayCalculatorType = @PayCalculatorType," +
                " MonthlySalary = @MonthlySalary" +
                " WHERE Id = @Id;";
            cmd.Parameters.Add(new SqlParameter("PayCalculatorType", c.GetType().Name));
            cmd.Parameters.Add(new SqlParameter("MonthlySalary", c.MonthlySalary));
            cmd.Parameters.Add(new SqlParameter("Id", e.Id));

            cmd.ExecuteNonQuery();
        }