执行 SQL 查询 WCF RIA Silverlight

发布于 2024-11-01 21:32:13 字数 1298 浏览 1 评论 0原文

我创建了一个数据库,并将其与 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());
        }

I have created a database, linked it with DomainService's within my Silverlight Application. Now I want to be able to perform certain actions, such as Registration, Login etc. by using the service.

How would I be able to do this. I have preset methods created in the service, e.g. InsertUser but it only requires one parameter, so I'm not sure how it works. In the metadata I have all fields etc.

Can anyone help me out here.

Thanks.

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);
            }
        }

For retrieving User I have used (as a base from TBohnen.jnr code):

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());
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-11-08 21:32:13

这是添加一个新用户:

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 访问它

This is to add a new user:

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();

To retrieve your existing users:

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);
    }
}

This will trigger an async call that get's all the users and will add it to the DomainContext and you will be able to access it through dc.User

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文