Urgent ! Can't update the database by using DataAdapter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All Expert,

I have used the following code to update my mdb file by dataadapter, but it does not work, please help me.

Dim strDBPath As String
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim cmdbuilder As OleDbCommandBuilder

strDBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Demo\DEMO.mdb;Persist Security Info=False"

con = New OleDbConnection(strDBPath)
cmd = New OleDbCommand("select * from Staff where c_emp_no='8452'", con)

con.Open()

da = New OleDbDataAdapter(cmd)
ds.Tables.Add("Staff"

da.Fill(ds, "Staff"

Dim row As DataRow = ds.Tables(0).Rows(0)

row.BeginEdit()
row("c_emp_name") = "Michael"
row.EndEdit()

da = New OleDbDataAdapter(cmd)
cmdbuilder = New OleDbCommandBuilder(da)

da.UpdateCommand = cmdbuilder.GetUpdateCommand
da.UpdateCommand = cmdbuilder.GetDeleteCommand
da.UpdateCommand = cmdbuilder.GetInsertCommand
ds.AcceptChanges()
TextBox1.Text = da.Update(ds, "staff"

Then I check the mdb file, the field ("c_emp_name") does not changed to "Michael
Can anyone point out where should I modify ?

Thank you all
 
The best way in the world to ensure that your updates don't get submitted
back to the db is to call AcceptChanges right before you call update ;-)
Remove that line and it should work. Acceptchanges changes the rowstate to
unchanged and the way you have the code structured, you'll never have your
updates submitted. I have a full discussion here
http://www.knowdotnet.com/articles/efficient_pt4.html but remove that
line... if it doesn't fix it, let me know and we'll take it from there.

Cheers,

Bill
Kobeting said:
Dear All Expert,

I have used the following code to update my mdb file by dataadapter, but
it does not work, please help me.
Dim strDBPath As String
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim cmdbuilder As OleDbCommandBuilder


strDBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\Demo\DEMO.mdb;Persist Security Info=False"
 
Back
Top