コード例 #1
0
        //public void SaveEmployeeKnownType(EmployeeKnownType employee)
        public void SaveEmployeeKnownType(EmployeeInfo employee)
        {
            string cs = ConfigurationManager.ConnectionStrings["SampleWebService"].ConnectionString;

            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spSaveEmployeeKnownType", con);
                cmd.CommandType = CommandType.StoredProcedure;
                //SqlParameter parameterId = new SqlParameter
                //{
                //    ParameterName = "@Id",
                //    Value = employee.Id
                //};
                //cmd.Parameters.Add(parameterId);

                SqlParameter parameterName = new SqlParameter
                {
                    ParameterName = "@Name",
                    Value         = employee.Name
                };
                cmd.Parameters.Add(parameterName);

                SqlParameter parameterGender = new SqlParameter
                {
                    ParameterName = "@Gender",
                    Value         = employee.Gender
                };
                cmd.Parameters.Add(parameterGender);

                SqlParameter parameterDateOfBirth = new SqlParameter
                {
                    ParameterName = "@DateOfBirth",
                    //Value = employee.DateOfBirth
                    Value = employee.DOB
                };
                cmd.Parameters.Add(parameterDateOfBirth);

                SqlParameter parameterEmployeeType = new SqlParameter
                {
                    ParameterName = "@EmployeeType",
                    //Value = employee.EmployeeType
                    Value = employee.EmployeeType
                };
                cmd.Parameters.Add(parameterEmployeeType);

                //if (employee.GetType() == typeof(FullTimeEmployee))
                if (employee.EmployeeType == EmployeeType.FullTimeEmployee)
                {
                    SqlParameter parameterAnnualSalary = new SqlParameter
                    {
                        ParameterName = "@AnnualSalary",
                        //Value = ((FullTimeEmployee)employee).AnnualSalary
                        Value = employee.AnnualSalary
                    };
                    cmd.Parameters.Add(parameterAnnualSalary);
                }
                else
                {
                    SqlParameter parameterHourlyPay = new SqlParameter
                    {
                        ParameterName = "@HourlyPay",
                        //Value = ((PartTimeEmployee)employee).HourlyPay,
                        Value = employee.HourlyPay,
                    };
                    cmd.Parameters.Add(parameterHourlyPay);

                    SqlParameter parameterHoursWorked = new SqlParameter
                    {
                        ParameterName = "@HoursWorked",
                        //Value = ((PartTimeEmployee)employee).HoursWorked
                        Value = employee.HoursWorked
                    };
                    cmd.Parameters.Add(parameterHoursWorked);
                }

                lastSavedEmployee = employee.GetEmployeeObject();

                con.Open();
                cmd.ExecuteNonQuery();
            }
        }