我会说我是VB的新手,但我已经使用它超过一年,但主要是小型任务(主要是学校相关的)。无论如何,我当前的项目是我的A-Level任务,我需要能够添加,更新,删除和读取数据库。我当前的代码允许这种情况发生但在将新记录添加到我的数据库之后,VB中的数据集以不同方式显示行。我有3个桌子:" PlayerInfo"," PlayerSkill"和#34; PlayerAbilities"。何时添加新记录;新的" PlayerInfo"信息成为我的数据集中的第一行,而#34; PlayerSkill"和#34; PlayerAbilities"成为最后一排。这会导致所有信息不正确地匹配。我想知道是否有其他人有这个问题,他们是否知道如何解决它。
这显示了要添加到我的数据库的工作代码。
If inc <> -1 Then
Dim cb1 As New OleDb.OleDbCommandBuilder(da1)
Dim cb2 As New OleDb.OleDbCommandBuilder(da2)
Dim cb3 As New OleDb.OleDbCommandBuilder(da3)
Dim dsNewRow1 As DataRow
Dim dsNewRow2 As DataRow
Dim dsNewRow3 As DataRow
Try
dsNewRow1 = ds1.Tables("Players").NewRow()
dsNewRow1.Item("Forename") = Forename.Text()
dsNewRow1.Item("Surname") = Surname.Text()
ds1.Tables("Players").Rows.Add(dsNewRow1)
da1.Update(ds1, "Players")
dsNewRow3 = ds3.Tables("Players").NewRow()
dsNewRow3.Item("Reactions") = Reactions.Text()
dsNewRow3.Item("Strength") = Strength.Text()
dsNewRow3.Item("Speed") = Speed.Text()
dsNewRow3.Item("Stamina") = Stamina.Text()
dsNewRow3.Item("Accuracy") = Accuracy.Text()
dsNewRow3.Item("Coordination") = Coordination.Text()
ds3.Tables("Players").Rows.Add(dsNewRow3)
da3.Update(ds3, "Players")
dsNewRow2 = ds2.Tables("Players").NewRow()
dsNewRow2.Item("RegularShot") = RegularShot.Text()
dsNewRow2.Item("ShortServe") = ShortServe.Text()
dsNewRow2.Item("FlickServe") = FlickServe.Text()
dsNewRow2.Item("Clear") = Clear.Text()
dsNewRow2.Item("Smash") = Smash.Text()
dsNewRow2.Item("DropShot") = DropShot.Text()
ds2.Tables("Players").Rows.Add(dsNewRow2)
da2.Update(ds2, "Players")
MsgBox("New Record added to the Database")
Commit.Enabled = False
AddNew.Enabled = True
Update.Enabled = True
Delete.Enabled = True
Catch
MsgBox("Error")
Me.Close()
End Try
End If
这显示了在不同文本框中显示每个数据集记录的代码。
Try
Forename.Text = ds1.Tables("Players").Rows(inc).Item(1)
Surname.Text = ds1.Tables("Players").Rows(inc).Item(2)
Speed.Text = ds3.Tables("Players").Rows(inc).Item(3)
Strength.Text = ds3.Tables("Players").Rows(inc).Item(2)
Reactions.Text = ds3.Tables("Players").Rows(inc).Item(1)
Stamina.Text = ds3.Tables("Players").Rows(inc).Item(4)
Coordination.Text = ds3.Tables("Players").Rows(inc).Item(6)
Accuracy.Text = ds3.Tables("Players").Rows(inc).Item(5)
ShortServe.Text = ds2.Tables("Players").Rows(inc).Item(2)
FlickServe.Text = ds2.Tables("Players").Rows(inc).Item(3)
Clear.Text = ds2.Tables("Players").Rows(inc).Item(4)
Smash.Text = ds2.Tables("Players").Rows(inc).Item(5)
DropShot.Text = ds2.Tables("Players").Rows(inc).Item(6)
RegularShot.Text = ds2.Tables("Players").Rows(inc).Item(1)
Catch
MsgBox("Error")
Me.Close()
End Try
可悲的是,我现在没有足够的&#34; Rep&#34;上传数据集的图片。 如果这还不够,我很抱歉,我并不习惯使用论坛等。
答案 0 :(得分:0)
我不完全确定这是否算作答案,因为我仍然不知道在第一种情况下导致问题的原因。无论如何,通过编辑SQL语句以按主键的顺序显示行,我得到了我想要的代码。 ORDER BY [Player#]。我确实必须将名称更改为&#34; Player&#34;因为&#34; CommandBuilder&#34;不喜欢特殊字符。