MySqlBulkLoader 导入csv Incorrect datetime value: '' for column
错误信息
csv文件数字类型字段的值为空,导入报错
MySqlConnector.MySqlException:“Incorrect integer value: '' for column 'PaymentTerm' at row 1”
csv文件日期类型字段的值为空,导入报错
MySqlConnector.MySqlException:“Incorrect datetime value: '' for column 'LastUpdateDate' at row 1”
解决方案
将空值类型转换为NULL
将DataTable转换为标准的CSV字符串
C# 全选
/// <summary>
/// 将DataTable转换为标准的CSV字符串
/// </summary>
/// <param name="dataTable"></param>
/// <param name="createTitle">是否添加标题</param>
/// <returns></returns>
public static string ToCsv2(this DataTable dataTable, bool createTitle = false)
{
var builder = new StringBuilder();
// 添加列标题
if (createTitle)
{
var columnTitles = string.Join(",", dataTable.Columns.Cast<DataColumn>().Select(column => $"\"{column.ColumnName}\""));
builder.AppendLine(columnTitles);
}
// 添加数据行
foreach (DataRow row in dataTable.Rows)
{
var fields = row.ItemArray.Select(field => field is DBNull ? "NULL" : $"\"{field.ToString().Replace("\"", "\"\"")}\"");
var line = string.Join(",", fields);
builder.AppendLine(line);
}
return builder.ToString();
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网