Windows 窗体无法从我的数据库获取更新?
我最近才开始摆弄 Visual Basic Express 和 Sql 数据库。
我已经成功启动并运行了一个数据库,并且可以从中查询信息。我什至创建了一个表单,可以将新条目添加到我使用的表中。
第一个表单有一个组合框,其中列出了我的表中的玩家姓名。 Form2 允许您向表添加新名称,但我添加的任何内容都不会立即在 Form1 中更新。我必须重新启动程序才能看到新条目。即便如此,这些新条目似乎也不是永久性的,因为它们最终会消失。
我的 Form1 代码:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db = New PlayerTestDataContextDataContext()
Dim PlayerList = From List In db.Players
Select List.PlayerName
For Each PName In PlayerList
cbPList.Items.Add(PName)
Next
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
frmNewPlayer.Show()
End Sub
End Class
Form2 代码:
Private Sub btnCPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPlayer.Click
Dim db = New PlayerTestDataContextDataContext()
If txtNewPlayer.Text = "" Then
lbWarning.Text = "Please enter a name!"
Else
Dim Plyr As New Player With {
.PlayerName = txtNewPlayer.Text}
db.Players.InsertOnSubmit(Plyr)
db.SubmitChanges()
Me.Close()
End If
End Sub
不确定这里出了什么问题...任何帮助都会受到赞赏。如果我在这里忽略了一个明显的答案,请原谅我,我不确定我需要寻找什么。
Ive only recently starting fiddling with Visual Basic Express and Sql Databases.
Ive managed to get a database up and running, and can query information from it. I have even created a form that can add a new entry to the table im using.
The first form has a ComboBox that list the PlayerNames in my table. Form2 allows you to add a new name to the table, but anything I add isnt immediately updated in Form1. I have to relaunch the program to see the new entries. Even then, these new entries dont seem to be permanent as they eventually dissappear.
The code I have for Form1:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db = New PlayerTestDataContextDataContext()
Dim PlayerList = From List In db.Players
Select List.PlayerName
For Each PName In PlayerList
cbPList.Items.Add(PName)
Next
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
frmNewPlayer.Show()
End Sub
End Class
The code for Form2:
Private Sub btnCPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPlayer.Click
Dim db = New PlayerTestDataContextDataContext()
If txtNewPlayer.Text = "" Then
lbWarning.Text = "Please enter a name!"
Else
Dim Plyr As New Player With {
.PlayerName = txtNewPlayer.Text}
db.Players.InsertOnSubmit(Plyr)
db.SubmitChanges()
Me.Close()
End If
End Sub
Not sure what is going wrong here...any help is appreciated. If I have overlooked an obvious answer around here forgive me, Im not sure what I need to be looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那应该可以解决问题。但你需要读一些书...
That should do the trick. But you need to do some reading ...
您实际上从未告诉 Form1 更新其列表。
这就是现在发生的情况:
至此,您已经完成了操作流程,但 Form1 从未收到添加新 Player 的提示。也没有任何代码可以使列表重新加载,除非 Form1 启动(Form_Load 事件/方法仅在第一次加载时调用)。
您需要(选择一个):
模态方法是最简单的。
You never actually tell Form1 to update its list.
This is what is happening now:
At this point, you are done with your flow of operation, but Form1 has never been tipped off that a new Player has been added. Nor is there any code that would make the list reload except when Form1 launches (the Form_Load event/method is only called for the first time it is loaded).
You need (pick one):
The modal approach would be the easiest.