This is my code for btnUpdate so that msAccess will update.
private void btnUpdate_Click(object sender, EventArgs e)
{
string CoString=(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\AccessDatabase.accdb");
OleDbConnection con = new OleDbConnection(CoString);
string Update ="Select * from StudentDb";
DataSet ds = new DataSet();
DataSet changes;
OleDbCommandBuilder cbuild = new OleDbCommandBuilder();
try
{
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(Update, con);
da.Fill(ds);
cbuild = new OleDbCommandBuilder(da);
changes = ds.GetChanges();
if (changes != null)
{
da.Update(ds.Tables[0]);
}
ds.AcceptChanges();
MessageBox.Show("Save changes");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I am not having any error but when I click save and load the msAccess database it reverts back to original state, does not update at all in msAccess but on datagridview on c# it updates successfully, what could be wrong with my codes?
It doesn't appear that you are retrieving any data from the datagridview at all. You are retrieving all the contents from the StudentDB table in the Access database into DataSet ds. You then call GetChanges() on ds and save it into another DataSet called changes. Since the contents of ds come straight from the Access database there will be no changes to save.
来源:https://stackoverflow.com/questions/25785371/failed-to-update-in-msaccess-but-successfully-in-dgv-c-sharp