c# - 将两个csv文件导入mysql数据库

标签 c# mysql windows csv import

我需要将两个具有不同列的csv文件导入到mysql数据库表中。

当我导入第二个文件时,表中仍然包含第一个 csv 文件的数据,因此我需要一种方法或测试来更新数据(如果存在差异)并添加不存在的数据。

我的代码如下所示:

第一个文件的方法 Import_Bilan 和第二个文件的方法 Import_data。

 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=***;port=3306;username=root;password=root;SslMode=none;AllowUserVariables=true");
        var msbl = new MySqlBulkLoader(con)
        {
            TableName = "**",
            FieldTerminator = ";",
            FileName = dest,
            NumberOfLinesToSkip = 1,

        };
        msbl.Columns.AddRange(new[] { "***", "***""@discard", "@discard","@discard", "@discard", "@discard"});

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

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


private void Import_Data_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=***;port=3306;username=root;password=root;SslMode=none;AllowUserVariables=true");
        var msbl = new MySqlBulkLoader(con)
        {
            TableName = "****",
            FieldTerminator = ";",
            FileName = dest,
            NumberOfLinesToSkip = 1,

        };

        msbl.Columns.AddRange(new[] { "@discard", "***" });

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

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

最佳答案

根据传递给 msbl.Columns.AddRange 的列名称,您的两个输入 CSV 文件似乎具有非常不同的数据类型和列数。 (如果这不正确,请使用有关 CSV 文件结构的信息编辑您的问题。)

如果这是真的,我会建议两种不同的方法之一:

用 C# 读取 CSV

使用像 CsvHelper 这样的库使用 C# 读取两个 CSV 文件并将数据连接在一起(例如,通过基于共享 key 创建字典),然后将行(一次一行)插入 MySQL。这可能最终会变慢,并且可能需要更多代码。所以,你可以......

合并MySQL中的表

从上面的代码开始,但将 CSV 文件加载到两个临时表中;我们将它们命名为 exercices1exercices2。 (它们可以具有与练习相同的架构,或者如果您愿意,您也可以为每个 CSV 创建自定义架构。)

然后执行一条MySQL INSERT INTO声明:

INSERT INTO exercices (all, the, output, columns, that, you, want)
SELECT e1.column1, e1.column2, e2.column1, e2.column2
FROM exercices e1 JOIN exercices e2
    WHERE e1.some_column = e2.some_column;

显然,确切的详细信息将取决于 CSV 文件的确切结构、它们包含哪些数据、它们共有哪些列(您可以加入)等。

完成此操作后,您可以删除将 CSV 数据加载到的两个表。

关于c# - 将两个csv文件导入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365893/

相关文章:

c# - 如何在 HTML 中为 ASP.NET MVC 创建按钮?

android - 将通知从基于 Web 的应用程序推送到 Android

php - 如何正确书写MySQL别名?

windows - 如何在文件所有行的最后一个和最后一个单词之前插入逗号?

c# - Windows 8应用商店MediaElement事件未触发

c# - 数组中数字的排列

mysql - 使用存储过程从表中插入和选择

c++ - 如何在 Windows 上构建 lib2geom

python - 如何从 python 启用 Windows 控制台快速编辑模式?

c# - 接口(interface)中具有不同参数的策略模式 (C#)