LINQ:如何在多用户环境中更新列表框? [英] LINQ: How to update listbox in multi-user environment?

查看:65
本文介绍了LINQ:如何在多用户环境中更新列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to SQL构建一个简单的WPF应用程序,以从SQL Server 2008数据库检索数据并在列表框中列出它们.

我的数据库具有带有CustomerIdName列的Customer表.我使用O/R设计器来构建他们的datacontext类.

我只是有一个文本框和一个按钮,当单击该按钮时,会将在文本框中提供的客户名称添加到数据库中并更新列表框.

这个应用程式可成功为一位使用者运作.这是后面的代码(我从此示例中进行了修改,使用LINQ在WPF中绑定数据库的简单演示-SQL :-

I''m building a simple WPF application using LINQ to SQL to retrieve data from a SQL Server 2008 database and list them in a listbox.

My database has Customer table with CustomerId and Name columns. I used O/R designer to build their datacontext class.

I simply have a text box and a button which, when clicked, adds a customer''s name provided in the textbox to both the database and update the listbox.

This app work successfully for one user. And here is the code behind (which I modified from this example Simple Demo of Binding to a Database in WPF using LINQ-SQL:-

imports System.Collections.ObjectModel

Class Window1
    Private Shared _db As New DataClasses1DataContext()
    Private _knownCustomer As ObservableCustomer
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        _knownCustomer = New ObservableCustomer(_db)
        Me.ListBox1.ItemsSource = _knownCustomer
    End Sub

    Private Sub add_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Button1.Click
        Dim newCustomer As New Customer()
        newCustomer.Name = TextBox1.Text
        _db.Customers.InsertOnSubmit(newCustomer)
        _db.SubmitChanges()
        TextBox1.Text = ""
        _knownCustomer.Add(newCustomer)
    End Sub
End Class

Public Class ObservableCustomer
    Inherits ObservableCollection(Of Customer)
    Public Sub New(ByVal db As DataClasses1DataContext)
        For Each thisCustomer As Customer In db.Customers
            Me.Add(thisCustomer)
        Next
    End Sub
End Class


我的问题是,如果user1和user2都试图添加新的客户名,例如,user1要添加"Simon",然后user2要添加"Micheal",则user2是否可以(单击添加"时)添加到在他的列表框中同时看到"Simon"(由user1添加)和"Michael"(由他添加)?我的意思是最后一个用户单击添加按钮时将从数据库中获取更新的数据,而不是其本地可观察的集合.

对不起,英语不好,但是我希望这是可以理解的,请提供帮助. X |


My problem is, if user1 and user2 are both trying to add a new customer name, say, user1 wants to add "Simon" and then user2 want to add "Micheal", is it possible for user2 (when click "add") to see both "Simon" (added by user1) and also "Michael" (that is added by him) in his listbox? I mean the last user will get updated data from database, not their local observable collection when they click add button.

Sorry for poor english but I hope this is understandable and please help. X|

推荐答案

应用程序如何连接?是网站还是客户端应用程序?无论哪种方式,如果有一个中央数据存储,那么所有这些LINQ代码都是完全不相关的.所需要的是某种计时器或使者,以告知应用程序根据另一位用户对数据存储所做的更改进行更新.
How are the applications connected ? Is it a website or a client side app ? Either way, if there''s a central data store, all this LINQ code is utterly irrelevant. What''s needed is some sort of timer or messenger to tell the app to update based on changes made to the data store by another user.


这篇关于LINQ:如何在多用户环境中更新列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆