CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2002
    Location
    Ukraine
    Posts
    228

    Help. I cannot update mdb file.

    I try to do it in a simple example.
    File is present, opend.
    Data accessable.
    But no updates executed.
    And runtime errors are absent too ;_(

    OleDbConnection mConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\\Tmp.mdb");
    OleDbCommand cmd = new OleDbCommand("SELECT t1.s1 FROM t1;", mConn);
    mConn.Open();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataRow row = ds.Tables[0].NewRow();
    row["s1"] = "aaa";
    ds.Tables[0].Rows.Add(row);
    ds.AcceptChanges();
    OleDbCommandBuilder CommandBuilder = new OleDbCommandBuilder(da);
    da.InsertCommand = CommandBuilder.GetInsertCommand();
    da.Update(ds,"Table");
    mConn.Close();

  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Help. I cannot update mdb file.

    Simply remove that line of code:

    Code:
    ds.AcceptChanges();
    AcceptChanges method commits all changes made to dataset
    and marks all new rows as unchanged. What's why dataadapter
    doesn't updates anything. It's worth to mention that
    DataAdapter's Update method internally calls AcceptChanges for
    every row updated, therefore you don't need to call AcceptChanges
    on dataset.





    Quote Originally Posted by Alex Rest
    I try to do it in a simple example.
    File is present, opend.
    Data accessable.
    But no updates executed.
    And runtime errors are absent too ;_(

    OleDbConnection mConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\\Tmp.mdb");
    OleDbCommand cmd = new OleDbCommand("SELECT t1.s1 FROM t1;", mConn);
    mConn.Open();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataRow row = ds.Tables[0].NewRow();
    row["s1"] = "aaa";
    ds.Tables[0].Rows.Add(row);
    ds.AcceptChanges();
    OleDbCommandBuilder CommandBuilder = new OleDbCommandBuilder(da);
    da.InsertCommand = CommandBuilder.GetInsertCommand();
    da.Update(ds,"Table");
    mConn.Close();
    Best regards,
    Igor Sukhov

    www.sukhov.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured