Click here to Skip to main content
15,888,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can successfully import two tables from a paradox database and and add the the data into one access table.From the one paradox table I use the ID field as it is the primary key to link clockings to dates and I made my access primary key equal to the ID field of the Clockd.ID.

How do I append the access table when updating from the paradox database adding just new records
I am a little bit stumped on this
This is how far I have gotten.Any guidance would be appreciated

C#
private void mtrBtnLoad_Click(object sender, EventArgs e)
{
   string connStringSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Program Files\\Supertime\\db;Extended Properties=Paradox 5.x;";

   string connStringDest = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Yellow\\Documents\\paradoxExport.accdb; Persist Security Info=False;";

   OleDbConnection connSource = new OleDbConnection(connStringSource);
   OleDbConnection connDest = new OleDbConnection(connStringDest);

   OleDbCommand cmdSelect = new OleDbCommand(@"Select Clockd.ID, Empmas.EmpNo, Empmas.Name, Clockd.DateTime, Calc0R1, Calc1R1, Calc2R1, Calc3R1, Calc4R1, TotalHours, Target0R1 FROM Empmas INNER JOIN Clockd ON Empmas.EmpNo=Clockd.Empno", connSource);

   OleDbCommand cmdIns = new OleDbCommand(@"INSERT INTO Clockd ([ID], EmpNo, [Name], [DateTime], Calc0R1, Calc1R1, Calc2R1, Calc3R1, Calc4R1, TotalHours, Target0R1) VALUES (@ID, @EmpNo, @Name, @DateTime, @Calc0R1, @Calc1R1, @Calc2R1, @Calc3R1, @Calc4R1, @TotalHours, @Target0R1)", connDest);

   cmdIns.Parameters.Add(new OleDbParameter("@ID", OleDbType.VarChar, 11,"ID"));
   cmdIns.Parameters.Add(new OleDbParameter("@EmpNo", OleDbType.VarChar, 11, "EmpNo"));
   cmdIns.Parameters.Add(new OleDbParameter("@Name", OleDbType.VarChar, 11, "Name"));
   cmdIns.Parameters.Add(new OleDbParameter("@DateTime", OleDbType.Date, 4, "DateTime"));
   cmdIns.Parameters.Add(new OleDbParameter("@Calc0R1", OleDbType.Integer, 8, "Calc0R1"));
   cmdIns.Parameters.Add(new OleDbParameter("@Calc1R1", OleDbType.Integer,8, "Calc1R1"));
   cmdIns.Parameters.Add(new OleDbParameter("@Calc2R1", OleDbType.Integer, 8, "Calc2R1"));
   cmdIns.Parameters.Add(new OleDbParameter("@Calc3R1", OleDbType.Integer, 8, "Calc3R1"));
   cmdIns.Parameters.Add(new OleDbParameter("@Calc4R1", OleDbType.Integer, 8, "Calc4R1"));
   cmdIns.Parameters.Add(new OleDbParameter("@TotalHours", OleDbType.Integer, 4, "TotalHours"));
   cmdIns.Parameters.Add(new OleDbParameter("@Target0R1", OleDbType.Integer, 4, "Target0R1"));

   OleDbDataAdapter da = new OleDbDataAdapter(connStringDest,connStringDest); 
   da.AcceptChangesDuringFill = false;
   da.SelectCommand = cmdSelect;
   da.InsertCommand = cmdIns;

   System.Data.DataSet ds = new System.Data.DataSet();
   da.Fill(ds);
   da.Update(ds);
   DataTable dt = new DataTable();
   BindingSource bs = new BindingSource();
   bs.DataSource = dt;
   dvg1.DataSource = bs;
}
Posted
Updated 20-Mar-15 22:56pm
v2

1 solution

You'll need to get your insert query to check if the ID exists already before inserting the record.
Unfortunately to be able to put a WHERE clause into an INSERT query in Access requires that you use an INSERT INTO SELECT query.
This of course means that you need to have a table to select from, whereas you want to just give it some values to insert.
This makes the query a little bit longer and not quite as clear as it would be with something like SQL Server, but should still be possible.

Give this insert query a try:
C#
OleDbCommand cmdIns = new OleDbCommand(@"INSERT INTO Clockd ([ID], EmpNo, [Name], [DateTime], Calc0R1, Calc1R1, Calc2R1, Calc3R1, Calc4R1, TotalHours, Target0R1) SELECT TOP 1 @ID, @EmpNo, @Name, @DateTime, @Calc0R1, @Calc1R1, @Calc2R1, @Calc3R1, @Calc4R1, @TotalHours, @Target0R1 FROM Clockd WHERE NOT EXISTS (SELECT [ID] FROM Clockd WHERE [ID]=@ID)", connDest);


It does a select (limited to 1 row) that will return all the parameters, but only if the ID doesn't already exist in the table.
 
Share this answer
 
Comments
Gerhard_Louis 21-Mar-15 9:09am    
Hi thank you.It works.I added a manual timestamp in the paradox database and noted the unique id it generated and then I made sure there was no record with that unique id in my access table and updated it.And re updated it again and I was not getting the same primary key violation.Although updating 700 employees records takes forever
Member 11428137 21-Mar-15 9:27am    
That's good to hear. Yes unfortunately it probably is quite slow, as it by necessity only does one row at a time. Also I don't think it's able to optimize it by batching the commands. Is it a regular thing that you run this import, or more a one off/time to time thing?
Gerhard_Louis 21-Mar-15 9:37am    
Its just for one client.Unfortunately we are stuck with using a paradox database until we move over to Sql

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900