Click here to Skip to main content
15,742,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to import two csv files to mysql database table, but when I will import the second file I want to compare it to the first file inserted in the database, and if there is a difference in a row I will insert the row of the second file.

I have tried to import directly the second file, but the result in the database still with data of the first file also not organized.

First, I uploaded and save the first file, then when I uploaded the second file I didn't see in the database table the data correctly of the second file I want a method to update if there is a difference or add new rows if exist in the second file

Here's my method to import and save the first csv file

What I have tried:

private void Import_Bilan_Click(object sender, EventArgs e)
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
                string file = openFileDialog1.FileName;
                string[] f = file.Split('\\');

                // to get the only file name
                string fn = f[(f.Length) - 1];
                string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string dest = path + @"\upload\" + fn;

                //to copy the file to the destination folder
                File.Copy(file, dest, true);
                MessageBox.Show("File Uploaded !!");

                //to copy the file to the destination folder
                File.Copy(file, dest, true);

                MySqlConnection con = new MySqlConnection("datasource=localhost;database=altares;port=3306;username=root;password=root;SslMode=none;AllowUserVariables=true");
                var msbl = new MySqlBulkLoader(con)
                    TableName = "exercices",
                    FieldTerminator = ";",
                    FileName = dest,
                    NumberOfLinesToSkip = 1,

                msbl.Columns.AddRange(new[] { "siren", "designation", "annee", "@discard", "@discard", "@discard", "@discard", "@discard", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "Ak", "AL", "AM", "AN", "AO", "AP", "AQ", "AR","@discard", "AT", "AU", "AV", "AW", "AX", "AY", "CS", "CT", "CU", "CV", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS","YP" });


                MessageBox.Show("Bilan bind to database !!");
Updated 17-Sep-18 12:43pm

1 solution

1) Load each Csv into its own temporary table.
2) "Save" all the entries in "table 2".
3) "Save" all the entries in "table 1" that are NOT in table 2.

(I assume your entries have "keys"; how else would you "match" them.)
Share this answer

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