首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >执行SQL查询WCF RIA Silverlight

执行SQL查询WCF RIA Silverlight
EN

Stack Overflow用户
提问于 2011-04-17 23:12:34
回答 1查看 827关注 0票数 0

我已经创建了一个数据库,并将其与我的Silverlight应用程序中的DomainService相链接。现在我希望能够通过使用该服务来执行某些操作,如注册、登录等。

我怎么才能做到这一点。我在服务中创建了预置方法,例如InsertUser,但它只需要一个参数,所以我不确定它是如何工作的。在元数据中,我有所有的字段等等。

有人能帮帮我吗。

谢谢。

代码语言:javascript
复制
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代码的基础):

代码语言:javascript
复制
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());
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-17 23:26:14

这是为了添加一个新用户:

代码语言:javascript
复制
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();

要检索现有用户,请执行以下操作:

代码语言:javascript
复制
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访问它

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5694290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档