执行SQL查询WCF RIA Silverlight

本文关键字:RIA Silverlight WCF 查询 SQL 执行 | 更新日期: 2023-09-27 17:50:19

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

我怎么能做到这一点。我在服务中创建了预设方法,例如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());
        }

执行SQL查询WCF RIA Silverlight

这是添加一个新用户:

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访问它。用户