VB .NET和数据库

时间:2012-11-13 12:56:19

标签: database vb.net ms-access

尝试使用这段代码输入播放器名称,将其填入数据行,然后在数据库中更新。它在数据行中的currentley更新,但我无法获得更新数据库的正确代码。你能帮忙干什么。

  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

2 个答案:

答案 0 :(得分:1)

您OleDbDataAdapter未链接到实际命令,您似乎没有在代码中创建适配器。 See an example here

缺少像这样的一行

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

答案 1 :(得分:-1)

da.Update(ds, "BPAWA")之前

添加:

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