我使用WCF从实体框架查询这个,我想在手机客户端使用数据,我可以知道如何从WCF函数查询多个数据类型?
[OperationContract]
string LoginUser(string email, string password);
IService.cs
public string LoginUser(string email, string password)
{
string query = @"SELECT VALUE tblUser FROM MyEntities.tblUsers AS tblUser WHERE tblUser.email = @email AND tblUser.password = @password";
ObjectParameter[] parameters = new ObjectParameter[2];
parameters[0] = new ObjectParameter("email", email);
parameters[1] = new ObjectParameter("password", password);
using (var context = new SteamKingEntities())
{
ObjectQuery<string> results = context.CreateQuery<string>(query, parameters);
foreach (string result in results)
{
if (result != null)
{
return result;
}
}
}
return null;
}
IService.svc.cs
public Login()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.LoginUserCompleted += new EventHandler<LoginUserCompletedEventArgs>(_serviceClient_LoginUserCompleted);
}
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.LoginUserAsync(txtEmail.Text, txtPassword.Password);
}
private void _serviceClient_LoginUserCompleted(object sender, LoginUserCompletedEventArgs e)
{
if (e.Error == null && e.Result != null)
{
MessageBox.Show("Welcome " + e.Result + "!");
}
else
{
MessageBox.Show(e.Error.Message + " Couldn't Login, Please try again =D");
}
}
Login.xaml.cs
我想从Entity Framework返回整个表,并允许我在Window Phone端逐个处理。
我想我需要对IService方做一些修改,但我真的不知道怎么回事,因为我是手机开发新手,习惯了Linq to SQL。请你指导我如何为返回制作类型的东西。谢谢。
更新
我得到的错误There was an error while trying to deserialize parameter http://tempuri.org/:LoginUserResult. Please see InnerException for more details.
我将代码修改为WCF Custom Object中所述的方式但我无法反序列化...我猜这个问题来自WP部分,我怎样才能反序列化结果 ?
答案 0 :(得分:1)
您需要使用数据合同:
[DataContract]
public class ReturnType
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Pass { get; set; }
}
在服务中它看起来像这样:
[OperationContract]
Entities.ReturnType[] SelectTopUser(int countOfTop);
返回类型可以是实体框架类型
答案 1 :(得分:1)
您可以使用ref,也可以使用custom-objects