简体   繁体   中英

Updating locally stored database from changes to multiple data grids ( Visual studio C#, Access database using OLEDB)

I'm trying but failing greatly to update a locally stored database based on changes which occur to a data grid which contains a copy of the database. The reason I think i'm having issues is due to the database having two tables which I've put into two data grids but with all one data set. My knowledge of OLEDB is limited but I've created multiple programs with a update routines like the one below so I'm not sure how to adapt it too this new program.

Screenshot of tables in working program

Variables

    OleDbConnection Connection;
    OleDbDataAdapter oledbAdapter;
    OleDbCommandBuilder oledbCmdBuilder;
    //DataSet ds = new DataSet();
    string ConnectionString = null;
    int CurrentRow = 0;
    DataSet ds = new DataSet();

Database connecting - executed on load

private void database_datagrid_load()
    {
        string SQL1 = "SELECT * FROM tbl_Customers";       
        string SQL2 = "SELECT * FROM tbl_Jobs";                     
        Connection = new OleDbConnection(ConnectionString);         
        try                                                         
        {
            ds.Clear();
            Connection.Open();
            oledbAdapter = new OleDbDataAdapter(SQL1, Connection);
            oledbAdapter.Fill(ds, "tbl_Customers");
            oledbAdapter.SelectCommand.CommandText = SQL2;
            oledbAdapter.Fill(ds, "tbl_Jobs");
            oledbAdapter.Dispose();
            Connection.Close();

            database_datagrid_customer.DataSource = ds.Tables[0];
            database_datagrid_jobs.DataSource = ds.Tables[1];



        }
        catch(Exception ex)
        {
            MessageBox.Show(Convert.ToString(ex));
        }

    }

Only code for updating which didn't crash - its completes but no changes to the database are made

  Connection.Open();
            string SQL = "SELECT * FROM tbl_Jobs"; 
            OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now


            OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW); //cmdbuilder is set but never used not sure why  
            DataSet changes = ds.GetChanges();





            if (changes != null)
            {
                oledbAdapterNEW.Update(ds.Tables[0]);
            }
            ds.AcceptChanges();
            MessageBox.Show("Jobs Save Changes");

Not positive but i think it should be

if (changes != null)
{
    ds.Tables[0].AcceptChanges();
    //oledbAdapterNEW.Update(ds.Tables[0]);
}

Figured it out, this is the solution for those who need it. Updates both tables.

private void database_btn_updatedb_Click(object sender, EventArgs e)
    {

        Connection = new OleDbConnection(ConnectionString);
        DataSet changes = ds.GetChanges();
        try
        {
            Connection.Open();
            string SQL = "SELECT * FROM tbl_Jobs"; 
            OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now
            OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW); 


            if (changes != null)
            {
                oledbAdapterNEW.Update(ds,"tbl_Jobs");
                MessageBox.Show("Jobs Save Changes");
            }

            SQL = "SELECT * FROM tbl_Customers";
            oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection);
            oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW);

            if (changes != null)
            {
                oledbAdapterNEW.Update(ds, "tbl_Customers");
                MessageBox.Show("Customer Save Changes");
            }
            ds.AcceptChanges();

            Connection.Close();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM