Failed to update in msAccess but successfully in dgv C#

三世轮回 提交于 2019-12-02 16:40:13

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!