使用wcf服务Windows窗体应用程序

时间:2017-09-05 00:32:28

标签: c# wcf ado.net

我正在使用Windows窗体应用程序中的Wcf服务。我正在尝试从sql数据库创建基于用户emu类型的用户登录系统。当我在文本框中输入值1时,它应该将全时员工方法其他值2返回到文本框中它应该返回兼职员工方法但是它不能正常工作期待..

这是Employee类代码....

    [KnownType(typeof(FullTimeEmployee))]
    [KnownType(typeof(PartTimeEmployee))]
    [DataContract(Namespace = "http://pragimtech.com/Employee")]
    public class Employee
    {
        private int _id;
        private string _name;
        private string _gender;
        private DateTime _dateOfBirth;

        [DataMember(Order = 1)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember(Order = 2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [DataMember(Order = 3)]
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        [DataMember(Order = 4)]
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }

        [DataMember(Order = 5)]
        public EmployeeType Type { get; set; }
    }
    [DataContract(Name = "EmployeeType")]
    public enum EmployeeType
    {
        [EnumMember]
        FullTimeEmployee = 1,
        [EnumMember]
        PartTimeEmployee = 2
    }
}

这是我的全职和兼职员工类继承自Employee类...

     public class FullTimeEmployee : Employee
        {
            public int AnnualSalary { get; set; }
        }
      public class PartTimeEmployee : Employee
        {
            public int HourlyPay { get; set; }
            public int HoursWorked { get; set; }
        }

以下是根据员工类型获取员工方法来访问员工的方法......

 public Employee GetEmployee(int Id)
        {
            Employee employee = null;
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetEmployee1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter parameterId = new SqlParameter();
                parameterId.ParameterName = "@EmployeeType";
                parameterId.Value = Id;
                cmd.Parameters.Add(parameterId);
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    if ((EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee)
                    {
                        return employee;
                       }                    }
                    else if ((EmployeeType)reader["EmployeeType"] == EmployeeType.PartTimeEmployee)
                    {

                        return employee;
                       }
                }
            }
            return employee;
        }

这是我的Windows表单应用程序......

  private void button1_Click(object sender, EventArgs e)
        {
            MyService.HalifaxServiceClient myservice = new MyService.HalifaxServiceClient("NetTcpBinding_IHalifaxService");
            MyService.Employee employee = myservice.GetEmployee(Convert.ToInt32(txt1.Text));
            MyService.FullTimeEmployee ft = new MyService.FullTimeEmployee();

            if (employee == myservice.GetEmployee(Convert.ToInt32(txt1.Text).CompareTo(employee.Type)))
            {
                FulltimeEmployeeLinkActivites();
            }
            else if (employee == myservice.GetEmployee(Convert.ToInt32(txt1.Text).CompareTo(employee.Type)))
            {

                PartTimeEmployeeActivities();
            }
            else
            {
                label4.Text = "No infomation found";
            }


        }

以下是我运行应用程序时的屏幕截图... Click here to see the application

2 个答案:

答案 0 :(得分:1)

我在if / else中看到的问题是条件语句完全相同。您可以根据对象类型进行分支的一种方法是使用is关键字。

if (employee is FullTimeEmployee) 
{
    FulltimeEmployeeLinkActivites();
}
else if (employee is PartTimeEmployee)
{
    PartTimeEmployeeActivities();
}
else
{
    label4.Text = "No information found";
}

我还要补充一点,这不一定是最佳做法,但它应该能满足你的要求。

除此之外,返回员工实例的方法永远不会返回有效类型的员工。 GetEmployee方法似乎没有实例化员工实例。它看起来总是返回null。尝试返回正确类型的实例。您还需要使用所需的数据填充实例。

if ((EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee)
{
    return new FullTimeEmployee();
}
else if ((EmployeeType)reader["EmployeeType"] == EmployeeType.PartTimeEmployee)
{
    return new PartTimeEmployee();
} 

答案 1 :(得分:1)

您的元素中存在令人困惑的命名,并且“employee”类似乎未在您的数据读取器中填充。

“FullTimeEmployee”是您的一个类的名称,也是您的一个枚举的名称。所以设定你的病情是不安全的

(EmployeeType)reader["EmployeeType"] == EmployeeType.FullTimeEmployee

如果没有spGetEmployee的代码,我无法确定,但如果它从表中返回具有相同名称的值,则声明更安全

while(reader.Read())
{
    employee= new employee();
    employee.Id= reader.GetInt32(0);
    employee.Name= reader.GetString(1);
    ...
    employee.EmployeeType=(EmployeeType)reader.GetInt32(4);

    if(employee.EmployeeType== EmployeeType.FullTimeEmployee)
    {
        //Do extra work for this type of employee
        ...
        return employee;
    }

}