简体   繁体   中英

VB.Net OleDBConnection code to update access database from DataGridView

I have a simple userform with a DataGridView and I would like to use OleDB code to update the accdb database based on any entries made in the gridview. The load button works fine, but the save button produces this error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

Here is my code:

Imports System.Data.OleDb

Public Class Form1

    Dim myConString As String
    Dim con As OleDbConnection = New OleDbConnection
    Dim Dadapter As OleDbDataAdapter
    Dim DSet As DataSet
    Dim DSet2 As DataSet
    Dim ConCMD As OleDb.OleDbCommand

    Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click

        myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Psmccsfs01\snd\PRODUCTION\Licensed\Reporting\FTO Adjustment Tools\FTO_Log_DB.accdb;"
        con.ConnectionString = myConString
        con.Open()
        Dadapter = New OleDbDataAdapter("select * from FTO_Log", con)
        DSet = New DataSet
        Dadapter.Fill(DSet, "FTO_Log")
        DataGridView1.DataSource = DSet.Tables("FTO_Log")
        con.Close()

    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click

        Using con = New OleDbConnection(myConString)
            con.Open()
            Dadapter.Update(DSet, "FTO_Log")
        End Using

    End Sub

End Class
dataAdpater.UpdateCommand = new SqlCommand(
           "UPDATE Categories SET CategoryName = @CategoryName " +
           "WHERE CategoryID = @CategoryID", connection);

notice the typo in the code above, straight from the msdn site ;p

short and simple: http://msdn.microsoft.com/en-us/library/33y2221y%28v=vs.110%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM