消费wcf服务Windows窗体应用程序
我正在使用Windows窗体应用程序中的wcf服务。我正在尝试从sql数据库创建基于用户emu类型的用户登录系统。当我将值1输入到文本框中时,它应该返回全职雇员方法,否则将值2写入文本框中它应该返回兼职雇员方法,但它不工作期待..消费wcf服务Windows窗体应用程序
这里是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; }
}
这里是方法获取Employee方法来访问emp基于员工类型loyee ...
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";
}
}
我在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();
}
在这种情况下,返回Employee的方法不会返回正确类型的雇员。 –
查看已更新的答案 –
您的元素中有一个令人困惑的名称,而“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;
}
}
它闻到错误。您正在返回一个int值的列,其值与您作为参数传递的值相同,而不是员工记录或其Id。因此,期望总是回报你所要求的相同价值。 –
您的错误是您将Employee ID传递给函数GetEmployee,然后在只与EmployeeType进行比较的存储过程中使用Id。您的sp应该是类似于SELECT * FROM tblEmployee WHERE Id = @ EmployeeId –
reader(0)中预期的方法名称;这里 ..哪个方法名称期待 – Rasel
我如何删除此问题? – Rasel
为什么这个问题被拒绝? – Rasel