c# - 将两个csv文件导入mysql数据库
问题描述
我需要将两个具有不同列的 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 文件并将数据连接在一起(例如,通过Dictionary
基于共享键创建一个),然后将行(一次一个)插入 MySQL。这可能最终会变慢,并且可能需要更多代码。所以,你可以……</p>
合并 MySQL 中的表
从上面的代码开始,但将 CSV 文件加载到两个临时表中;让我们打电话给他们exercices1
和exercices2
。(它们可以具有与 相同的架构exercices
,或者您可以根据需要为每个 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 数据加载到其中的两个表。
推荐阅读
- javascript - 如何使用 javascript 从 django FileResponse 获取图像
- python - 如何将文本文件写入 syslog.1.gz 文件?
- rust - Rust 构建脚本将文件复制到目标目录
- python - 在 Python 中使用线程计算阶乘
- reactjs - 我的 react native tensorflow 模型(我使用 tfjs 转换器从 keras 转换而来)对每张图像都给出了相同的预测
- azure-cosmosdb - 比较 Cosmos DB (SQL API) 中的数组属性
- sql - 在sql server的一个视图中组合多个UNION
- c - 如何获取带有前置字符的输入?
- c# - 如何解决sql server中的数据类型转换?
- android - buildFeatures 是不稳定的,因为它的签名引用了不稳定的 ... 标有 @Incubating