使用带有封闭和空值的 SQLServerBulkCSVFileRecord 加载 CSV 数据
Loading CSV data using SQLServerBulkCSVFileRecord with enclosing and nulls
我正在尝试使用 SQLServerBulkCSVFileRecord 加载 CSV 数据,但是这似乎不允许包含字段(去除引号)并提供 NULL 值。
关于如何让它工作有什么建议吗?似乎加载 CSV 文件需要这样的基本功能。
我目前使用的代码:
final SQLServerBulkCopy copymanager = new SQLServerBulkCopy(connection);
final SQLServerBulkCSVFileRecord csv = new SQLServerBulkCSVFileRecord(datafile.toString(), true);
final ResultSet resultSet = connection.createStatement().executeQuery(
String.format("select * from [%s].[%s]", getSchema(), tableName));
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
csv.addColumnMetadata(i, resultSet.getMetaData().getColumnName(i), resultSet.getMetaData().getColumnType(i),
resultSet.getMetaData().getPrecision(i), resultSet.getMetaData().getScale(i));
}
copymanager.setDestinationTableName(tableName);
copymanager.writeToServer(csv);
有趣的是,SQLServerBulkCSVFileRecord 的来源有一个 comment
- Both BCP and BULK INSERT considers double quotes as part of the data and throws error if any data (say "10") is to be
- inserted into an numeric column. Our implementation does the same.
这意味着您必须解析代码中的每一行以取消引用引用的值。
至于为 NULL 提供值,您可以在代码中执行此操作,我们在 table 中提供默认值。您的选择。
我正在尝试使用 SQLServerBulkCSVFileRecord 加载 CSV 数据,但是这似乎不允许包含字段(去除引号)并提供 NULL 值。
关于如何让它工作有什么建议吗?似乎加载 CSV 文件需要这样的基本功能。
我目前使用的代码:
final SQLServerBulkCopy copymanager = new SQLServerBulkCopy(connection);
final SQLServerBulkCSVFileRecord csv = new SQLServerBulkCSVFileRecord(datafile.toString(), true);
final ResultSet resultSet = connection.createStatement().executeQuery(
String.format("select * from [%s].[%s]", getSchema(), tableName));
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
csv.addColumnMetadata(i, resultSet.getMetaData().getColumnName(i), resultSet.getMetaData().getColumnType(i),
resultSet.getMetaData().getPrecision(i), resultSet.getMetaData().getScale(i));
}
copymanager.setDestinationTableName(tableName);
copymanager.writeToServer(csv);
有趣的是,SQLServerBulkCSVFileRecord 的来源有一个 comment
- Both BCP and BULK INSERT considers double quotes as part of the data and throws error if any data (say "10") is to be
- inserted into an numeric column. Our implementation does the same.
这意味着您必须解析代码中的每一行以取消引用引用的值。
至于为 NULL 提供值,您可以在代码中执行此操作,我们在 table 中提供默认值。您的选择。