在失败,但MSACCESS成功DGV C#更新 [英] Failed to update in msAccess but successfully in dgv C#

查看:170
本文介绍了在失败,但MSACCESS成功DGV C#更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我btnUpdate代码,使MSACCESS将更新。

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



我没有任何错误,但是当我点击保存和加载MSACCESS数据库它恢复到原来的状态,不MSACCESS但在C#中成功地更新datagridview的所有更新,这可能是错我的代码?

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?

推荐答案

它不会出现你是从datagridview的所有检索任何数据。您检索所有从Access数据库StudentDB表中的内容复制到DataSet的DS。然后调用GetChanges()在DS并将其保存到另一个数据集被称为改变。由于DS的内容从Access数据库来直接将不会有变化进行保存。

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.

这篇关于在失败,但MSACCESS成功DGV C#更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆