1

试图让这段代码输入玩家姓名,将其填写在数据行中,然后在数据库中更新它。它当前在数据行中更新,但我无法正确获取代码来更新数据库。你能帮忙喝彩吗。

  Using da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Team1", con)
            da.Fill(ds, "BPAWA")

            txtFirstName.Text = ds.Tables("BPAWA").Rows(0).Item(1)

            T1P1 = InputBox("Name of Player 1")

            ds.Tables("BPAWA").Rows(0).Item(1) = T1P1

            ds.Tables("BPAWA").AcceptChanges()
            ds.AcceptChanges()

            da.Update(ds, "BPAWA")
            MsgBox("Player 1 Added Successfully")
        End Using
4

2 回答 2

1

您的 OleDbDataAdapter 没有链接到实际命令,您似乎也没有在代码中创建适配器。在此处查看示例

缺少这样的一行

Dim da As OleDbDataAdapter = new OleDbDataAdapter(selectCommand, connection);

所以我可以用这种方式重写你的代码(避免全局变量)

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    Dim dbProvider As String = "PROVIDER=microsoft.ace.oledb.12.0;"
    Dim dbSource As String = "Data Source = C:\BP_Table_Project.accdb"
    Dim ds as DataSet = new DataSet()

    Using con As OleDbConnection = new OleDbConnection()
        con.ConnectionString = dbProvider & dbSource
        con.Open()

        MsgBox("Connection With Database Established", 0, "Database Connection")

        Using da as OleDbDataAdapter = new OleDbDataAdapter("SELECT * FROM Team1", con)
            Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
            da.Fill(ds, "BPAWA")
            ......

            da.Update(ds, "BPAWA")
            MsgBox("Player 1 Added Successfully")
        End Using
        MsgBox("Connection With Database Closed", 0, "Database Connection")
    End Using
End Sub
于 2012-11-13T13:23:15.963 回答
-1

da.Update(ds, "BPAWA")

添加:

ds.Tables("BPAWA").AcceptChanges()
ds.AcceptChanges()
于 2012-11-13T13:22:49.510 回答