所有用户与数据库之间的数据通信

我目前正在为我公司开发一个程序,用于在用户之间传递各种信息,除此之外目前还不重要。 将要传达的事情之一是日常任务清单。 基本上,意图是有一个检查列表,所有用户之间进行同步,所以如果一个用户检查一个任务,其他人都可以看到任务已经完成。

到目前为止,我所做的是我创build了一个DataGrid对象来保存checkbox。 该框开始为空,但是当程序加载时,运行下面的代码,填充列表。

Private Sub chkListPop() If System.IO.File.Exists(chkListPath) Then Dim newChk As CheckBox Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim rng As Microsoft.Office.Interop.Excel.Range Dim r As Byte xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBooks = xlApp.Workbooks.Open(chkListPath) xlWorkSheet = xlWorkBooks.Worksheets("Tasks") rng = xlWorkSheet.Range("A2") r = 0 Do While rng.Offset(r).Value <> "" chkListContainer.Items.Add(New Controls.CheckBox) newChk = chkListContainer.Items.Item(r) newChk.Content = rng.Offset(r).Value Try If rng.Offset(r, 1).Value = True Then newChk.IsChecked = True End If Catch End Try r = r + 1 Loop xlApp.DisplayAlerts = False xlWorkBooks.Save() xlWorkBooks.Close() xlApp.DisplayAlerts = True xlApp.Quit() End If End Sub 

基本上,子读取一个excel文件,并为文件中的每一行,创build一个checkbox与第二列中的真假值。 我想,要做到这一点,必须有一个更优雅的解决scheme。 我不是一个培训的程序员,所以我对这个“复杂”的主题知之甚less。 除了使用excel文件,还有更好的方法吗? 我应该使用Access数据库还是使用SQL数据库? 这个代码有效,但我觉得它错了。

另外,当有人检查任务时,我很难弄清楚如何更新文件。 现在我只需要一个定时器运行,每隔X分钟,程序就会更新excel文件。 这是如何工作的代码:

 Public Sub chkListSendData() If System.IO.File.Exists(chkListPath) Then Dim newChk As CheckBox Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim rng As Microsoft.Office.Interop.Excel.Range Dim r As Byte xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBooks = xlApp.Workbooks.Open(chkListPath) xlWorkSheet = xlWorkBooks.Worksheets("Tasks") rng = xlWorkSheet.Range("A2") r = 0 Do While rng.Offset(r).Value <> "" Try newChk = chkListContainer.Items.Item(r) Catch chkListContainer.Items.Add(New Controls.CheckBox) newChk = chkListContainer.Items.Item(r) newChk.Content = rng.Offset(r).Value End Try If newChk.IsChecked Then rng.Offset(r, 1).Value = True End If r = r + 1 Loop xlApp.DisplayAlerts = False xlWorkBooks.Save() xlWorkBooks.Close() xlApp.DisplayAlerts = True xlApp.Quit() End If End Sub Public Sub chkListGetData() If System.IO.File.Exists(chkListPath) Then Dim newChk As CheckBox Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim rng As Microsoft.Office.Interop.Excel.Range Dim r As Byte xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBooks = xlApp.Workbooks.Open(chkListPath) xlWorkSheet = xlWorkBooks.Worksheets("Tasks") rng = xlWorkSheet.Range("A2") r = 0 Do While rng.Offset(r).Value <> "" Try newChk = chkListContainer.Items.Item(r) Catch chkListContainer.Items.Add(New Controls.CheckBox) newChk = chkListContainer.Items.Item(r) newChk.Content = rng.Offset(r).Value End Try Try If rng.Offset(r, 1).Value = True Then newChk.IsChecked = True End If Catch End Try r = r + 1 Loop xlApp.DisplayAlerts = False xlWorkBooks.Close() xlApp.DisplayAlerts = True xlApp.Quit() End If End Sub Private Sub chkTimer() Dim dispatcherTimer As DispatcherTimer = New DispatcherTimer() AddHandler dispatcherTimer.Tick, AddressOf OnTimedEvent dispatcherTimer.Interval = New TimeSpan(0, 1, 0) dispatcherTimer.Start() End Sub Private Sub OnTimedEvent(ByVal sender As Object, ByVal e As EventArgs) chkListGetData() chkListSendData() End Sub 

在我的testing中,这一切都运行良好,但我有一种感觉,当很多人使用这个程序时,如果多个人同时尝试更新这个文件,这种方法就不会奏效。 如果我错了,这个代码是完美的,那太棒了,但我怀疑是这样。 如果有更好的方法来处理这种事情,我很乐意学习如何在这个程序中实现它。

在此先感谢您的帮助。