如何解决执行SQL查询WCF RIA Silverlight
| 我已经创建了一个数据库,并将其与Silverlight应用程序中的DomainService \链接。现在,我希望能够通过使用该服务执行某些操作,例如注册,登录等。 我将如何做到这一点。我在服务中创建了预设方法,例如InsertUser,但是它只需要一个参数,所以我不确定它是如何工作的。在元数据中,我具有所有字段等。 有谁可以帮我离开这里吗。 谢谢。public IQueryable<User> GetUsers()
{
return this.ObjectContext.Users;
}
public void InsertUser(User user)
{
if ((user.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(user,EntityState.Added);
}
else
{
this.ObjectContext.Users.Addobject(user);
}
}
为了检索用户,我使用了(作为TBohnen.jnr代码的基础):
UserContext _userContext = new UserContext();
public MainPage()
{
InitializeComponent();
LoadOperation loGetUsers = _userContext.Load(_userContext.GetUsersQuery());
loGetUsers.Completed += new EventHandler(loGetUsers_Completed);
}
void loGetUsers_Completed(object sender,EventArgs e)
{
LoadOperation<Web.User> lo = (LoadOperation<Web.User>)sender;
var user = _userContext.Users;
MessageBox.Show(user.ToString());
}
解决方法
这是要添加一个新用户:
YourDomainContext dc = new YourDomainContext();
User userToAdd = new User();
//You will have to set your properties here as I don\'t know them,I will give an example.
userToAdd.username = \"NewUser\";
dc.User.Add(userToAdd);
dc.SubmitChanges();
检索现有用户:
YourDomainContext dc = new YourDomainContext();
LoadOperation loGetUsers = dc.Load(dc.GetUsersQuery());
loGetUsers.Completed += new EventHandler( loadOperation_Completed );// You will see there is a callback overloads as well
and then add this as well.
private void loadOperation_Completed( object sender,EventArgs e )
{
LoadOperation<User> lo = (LoadOperation<User>)sender;
//Have a look at all the properties like lo.Error etc. but to see the retrieved users you can either use:
var users = lo.AllEntities;
//or if you declared your domaincontext as a class level parameter:
var users = dc.User;
foreach (Web.User user in users)
{
MessageBox.show(user.username);
}
}
这将触发一个异步调用,该调用将获取所有用户,并将其添加到DomainContext中,您将能够通过dc.User访问它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。