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();
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();