c# - 在 C# 中将重复记录从 excel 丢弃到 Mysql
问题描述
我有个问题:
例如,我的 excel 表如下所示:
Reference | Amount | Qty
----------------------------------
541254 | 93 | 0.60
103485 | 9377 | 0.80
021645 | 9375 | 0.80
021645 | 9375 | 0.80
我想将一个 excel 文件导入 mysql 数据库,但有些记录是重复的。我想要做的是检查某个或多个字段中的记录是否重复。如果一条记录重复,我将一条记录加载到 DDBB 中并忽略另一条记录。我有一种方法可以进行检查,但它给了我一个失败的类型:
“MySql.Data.MySqlClient.MySqlException:'您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 '' 附近使用的正确语法”
这是我的代码,但我不知道我可能有什么问题。帮我。
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using MySql.Data.MySqlClient;
using SpreadsheetLight;
public static string LoadToMySQL(string file, string table)
{
// file = @"C:\Users\Documents\excel_01.xlsx" path to my file
// table = table of my BBDD
program myobject = new program(); //Reference to the method "duplicate record"
string reply = "";
SLDocument sl = new SLDocument(file);
int i = 2;
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = configuracion.conexion;
connection.Open();
while (!string.IsNullOrEmpty(sl.GetCellValueAsString(i, 1)))
{
string Reference = sl.GetCellValueAsString(i,1);
string Amount = sl.GetCellValueAsString(i,2);
// validate code(LINK_REFERENCE) and to know if it exists in the table in order to: discard it or insert it.
if (myobject.datosExistentes(reference, amount)) //Reference to the method "duplicate record"
{
createLog("the reference exists: " + Reference + " with the amount: " + Amount);
}
else
{
string query = "INSERT INTO "+ table + "(REFERENCE, AMOUNT, QTY)" +
"VALUES(@reference, @amount, @qty)";
try
{
MySqlCommand mycmd = new MySqlCommand(query, connection);
mycmd.Connection = connection;
mycmd.Parameters.Clear();
mycmd.Parameters.AddWithValue("@reference", sl.GetCellValueAsString(i, 1));
mycmd.Parameters.AddWithValue("@amount", sl.GetCellValueAsString(i, 2));
mycmd.Parameters.AddWithValue("@qty", sl.GetCellValueAsString(i, 3));
mycmd.ExecuteNonQuery();
reply = "OK";
}
catch (MySqlException e)
{
createLog(e.ToString());
}
}
i++;
}
return reply;
}
private bool duplicateRecords(string reference, string amount)
{
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = configuracion.conexion;
connection.Open();
string query = "SELECT " + reference + " FROM MY_DDBB WHERE (amount ='" + amount+ "';";
MySqlCommand mycmd = new MySqlCommand(query, connection);
int num = Convert.ToInt32(mycmd.ExecuteScalar()); //here i get error!!!
if (num > 0)
{
return true;
}
else
{
return false;
}
public static void createLog(string logMessage)
{
.....
}
解决方案
如果您向要用于重复检测的一个或多个字段添加一个唯一键,那么您可以通过利用 MySQLON DUPLICATE KEY UPDATE
函数(即)简单地执行一个查询来完成这项工作:
string query = "INSERT INTO "+ table + " (REFERENCE, AMOUNT, QTY) " +
"VALUES (@reference, @amount, @qty) " +
"ON DUPLICATE KEY UPDATE QTY=VALUES(QTY);";
这将产生对相同值的不必要的无害更新,但避免在一个命令中全部重复插入,并且代码中不需要进一步的逻辑。
只需记住在表模式中为要考虑的一个或多个字段定义唯一键,以便 MySQL 可以检测到重复。
推荐阅读
- python - 通过正则表达式拆分word文档,然后将类似标题分组到自己的对象中
- php - Wordpress:array_push 在前一个数组中创建数组
- crystal-reports - 关于如何构建 BI 和 Crystal Reports 的基本了解的建议?
- excel - 如何将数据从excel加载到word
- express - findOne 不是函数
- entity-framework - Why are these two anonymous types not the same?
- google-apps-script - 在应用程序脚本中提及活动驱动器文件夹/当前驱动器文件夹
- javascript - 我希望我的导航在向下滚动时固定在某个位置?
- google-cloud-storage - 查询旧的 Google Cloud Datastore 记录时,foo__icon 的 QuerySet 为空
- bash - 循环中的 Bash 脚本表达式语法崩溃