cross-queries with two OleDbDataAdapters

0

I am trying to insert records in an Access table using an OleDbDataAdapter.

The data to be inserted is previously retrieved from another table in the same Access database with another OleDbDataAdapter, but when I run the Update method of the target OleDbDataAdapter, it does not insert any record.

This is the creation code of the DataAdapters:

private static OleDbDataAdapter DataAdapter()
    {
        try
        {
            OleDbConnection con = GestionBD.Con;
            OleDbDataAdapter oleDataAdapter = new OleDbDataAdapter();
            string cmd = "SELECT Id, Nombre, Apellidos, Direccion, NombreCompleto, Telefono, FechaAlta, FechaBaja, FechaUltimoPedido, ImporteUltimoPedido, TotalPedidos, Observaciones FROM Clientes WHERE FechaBaja IS NOT NULL";
            oleDataAdapter.SelectCommand = new OleDbCommand(cmd, con);
            OleDbCommandBuilder cmdbuilder = new OleDbCommandBuilder(oleDataAdapter);

            oleDataAdapter.InsertCommand = cmdbuilder.GetInsertCommand();
            oleDataAdapter.UpdateCommand = cmdbuilder.GetUpdateCommand();
            oleDataAdapter.DeleteCommand = cmdbuilder.GetDeleteCommand();

            return oleDataAdapter;
        }
        catch (OleDbException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw ex2;
        }
    }

private static OleDbDataAdapter DataAdapterHistoricos()
    {
        try
        {
            OleDbConnection con = GestionBD.Con;
            OleDbDataAdapter oleDataAdapter = new OleDbDataAdapter();
            string cmd = string.Empty;
            cmd = "SELECT Id, Nombre, Apellidos, Direccion, NombreCompleto, Telefono, FechaAlta, FechaBaja, FechaUltimoPedido, ImporteUltimoPedido, TotalPedidos, Observaciones FROM HistoricoClientes";                
            oleDataAdapter.SelectCommand = new OleDbCommand(cmd, con);
            OleDbCommandBuilder cmdbuilder = new OleDbCommandBuilder(oleDataAdapter);

            oleDataAdapter.InsertCommand = cmdbuilder.GetInsertCommand();
            oleDataAdapter.UpdateCommand = cmdbuilder.GetUpdateCommand();
            oleDataAdapter.DeleteCommand = cmdbuilder.GetDeleteCommand();

            return oleDataAdapter;
        }
        catch (OleDbException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw ex2;
        }
    }

This is where I perform the recovery of records and its dump in the 2nd DataAdapter:

DataTable c = new DataTable();

        OleDbDataAdapter da = DataAdapter();
        da.Fill(c);

        if (c.Rows.Count > 0)
        {
            OleDbDataAdapter daH = DataAdapterHistoricos();
            foreach (DataRow d in c.Rows)
            {
                d.SetModified();
            }

            int cH = daH.Update(c);

In the foreach I set the RowState of each row to Modified because if I do the Update directly it does not return any affected rows, so it returns the number of rows that theoretically updated / inserted, but as I said it does not insert any row.

I hope someone can shed some light on the subject, the truth is that I have not worked with Access for a long time and I am ignoring some obviousness.

Greetings.

    
asked by Miguel Ales 18.07.2018 в 17:09
source

0 answers