VB.NET + ACCESS数据库更新错误

时间:2013-12-09 13:11:34

标签: vb.net datagridview ms-access-2007

您好,首先感谢您的时间

我正在编写一个使用Acces数据库的应用程序

表格玩家

id玩家密码转动

1新'空'真3

表格地图

id Regio Eigenaar Troepen

1阿拉斯加“空”0

2 NoordCanada“空”0

...

40 IndoChina“空”0

在我的应用程序中我首先添加3(添加3个玩家)然后我将数据集放在DataGridView中,这样你就可以调整他们拥有的区域的玩家名称等等。

这一切都发生在以下代码

Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Public da As New OleDb.OleDbDataAdapter
Public ds As New DataSet
Dim sql As String
Public cb As New OleDb.OleDbCommandBuilder(da)

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    dbProvider = "PROVIDER=microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = "

    con.ConnectionString = dbProvider & dbSource & Form1.dbPath & "\Risk.accdb"

    con.Open()

    sql = "SELECT * FROM Map"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Map")

    Sql = "SELECT * FROM Players"
    da = New OleDb.OleDbDataAdapter(Sql, con)
    da.Fill(ds, "Players")

    con.Close()

    For i = 2 To Form3.MaxPlayersBox.Maximum

        ds.Tables("Players").Rows.Add(i)
        'Dim cb As New OleDb.OleDbCommandBuilder(da)

        'da.Update(ds, "Players")

    Next i

    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "Players"

End Sub

Private Sub PLayersB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PLayersB.Click

    DataGridView1.DataMember = Nothing

    DataGridView1.DataMember = "Players"

End Sub

Private Sub MapB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MapB.Click

    DataGridView1.DataMember = Nothing

    DataGridView1.DataMember = "Map"

    RandomizeB.Visible = True

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RandomizeB.Click

    If MsgBox("heb je alle spelers toegevoegd?", vbYesNo) = vbNo Then

        Exit Sub

    End If

    Dim P As Integer = 0
    Dim RandInt As New Random
    Dim Rand As Integer

    For i = 0 To 39

        Rand = RandInt.Next(0, 39)

line34:     If ds.Tables("Map").Rows(Rand).Item("Eigenaar").ToString = "" Then

            ds.Tables("Map").Rows(Rand).Item("Eigenaar") = ds.Tables("Players").Rows(P).Item("Players")

        ElseIf Rand < 39 Then

            Rand += 1
            GoTo Line34

        Else

            Rand = 0
            GoTo line34

        End If

        P += 1

        If P > ds.Tables("Players").Rows.Count - 1 Then

            P = 0

        End If

    Next i

    RandomizeB.Visible = False


End Sub

Private Sub SaveB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveB.Click

    con.Open()
    sql = "SELECT * FROM Map"
    da = New OleDb.OleDbDataAdapter(sql, con)
    Dim cb As New OleDb.OleDbCommandBuilder(da)

    da.Update(ds, "Map")

    sql = "SELECT * FROM Players"
    da = New OleDb.OleDbDataAdapter(sql, con)
    Dim cb2 As New OleDb.OleDbCommandBuilder(da)

    da.Update(ds, "Players")

    'The problem is right here 


    Me.Close()

End Sub

所以我更新的数据集已成为

表格玩家

id玩家密码转动

1 Player1'flash'true 3 2 Player2'空'假0 3 Player3'空'假0 4 Player4'空'假0

表格地图

id Regio Eigenaar Troepen

1阿拉斯加“Player2”0

2 NoordCanada“Player3”0

...

40 IndoChina“Player1”0

当我尝试更新数据库(私有子SaveB_Click)时,我收到一个错误:

da.Update(DS, “玩家”): 指令INSERT INTO包含语法错误

我已经在互联网上研究了一个星期,但我只是不知道该怎么做

数据库保存在共享的Dropbox文件夹BTW中。 (这就是为我的游戏提供在线多人游戏功能的原因)

感谢U,如果您需要更多信息,我将在当天余下的时间内提供。

1 个答案:

答案 0 :(得分:2)

这两个街区错了:

con.Open()
sql = "SELECT * FROM Map"
da = New OleDb.OleDbDataAdapter(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)

da.Update(ds, "Map")

添加新行的SQL需要SQL INSERT INTO语句,但看起来您粘贴了SELECT语句。 INSERT INTO将需要更多信息,例如列和要放在哪些值。我不能告诉你SQL应该是什么,因为我不知道列是什么或者你想把它放在那里。

FWIW,还有一个UPDATE语句,您可以在其中更改现有行的值,这也是不同的。