比较两个具有相同列的csv文件 [英] Compare two csv files with same columns

查看:85
本文介绍了比较两个具有相同列的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个csv文件导入到mysql数据库表中,但是当我将导入第二个文件时,我想将它与插入数据库中的第一个文件进行比较,如果行中存在差异,我将插入第二个文件的行。



我试图直接导入第二个文件,但数据库中的结果仍然是第一个文件的数据也没有组织。



首先,我上传并保存第一个文件,然后当我上传第二个文件时,我没有在数据库表中看到我想要的第二个文件的数据正确如果存在差异则更新的方法或添加新行(如果存在于第二个文件中)



这是导入和保存第一个csv文件的方法



我尝试过:



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

                msbl.Load();
                con.Close();

                MessageBox.Show("Bilan bind to database !!");
            }
        }

推荐答案

1)将每个Csv加载到自己的临时表中。

2)保存表2中的所有条目。

3)保存表2中不在表2中的所有条目。



(我假设你的参赛作品有钥匙;你怎么会匹配它们。)
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.)


这篇关于比较两个具有相同列的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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