来自 CSV 的 OracleBulkCopy - 无法插入空列具有值
OracleBulkCopy from CSV - Can not insert null column has value
我正在使用 OracleBlukCopy 将一个巨大的 CSV 文件转储到数据库中 table。这是我正在使用的代码。当我 运行 时,我收到一条错误消息,提示无法将 NULL 插入 TBL_COL1,但我的 CSV 具有 TBL_COL1.
的值
经过研究,我得出结论,当 TBL_COL1 中有字符时会出错。知道为什么吗?
DataTable datatable = TranslateCSVFileToDataTable();
using (OracleConnection connectiontodb = new OracleConnection(databaseconnectionstring))
{
connectiontodb.Open();
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
copytothetable.ColumnMappings.Add("TBL_COL1", "TBL_COL1");
copytothetable.ColumnMappings.Add("TBL_COL2", "TBL_COL2");
copytothetable.ColumnMappings.Add("TBL_COL3", "TBL_COL3");
copytothetable.DestinationTableName = "DESTINATION_TABLE";
copytothetable.WriteToServer(datatable);
}
}
CREATE TABLE DESTINATION_TABLE
(
TBL_COL1 VARCHAR2(32) not null,
TBL_COL2 DATE not null,
TBL_COL3 DATE not null,
)
98987987987987987,6/23/2014,7/23/2014
98987987987987987,7/23/2014,8/21/2014
98987987987987987,8/21/2014,9/22/2014
98987987987987987,9/22/2014,10/21/2014
98987987987987987,10/21/2014,11/20/2014
98987987987987987,11/20/2014,12/22/2014
656666666666666ABC1234,1/8/2014,1/9/2014
656666666666666ABC1234,1/9/2014,2/9/2014
656666666666666ABC1234,2/9/2014,3/9/2014
656666666666666ABC1234,3/9/2014,4/7/2014
656666666666666ABC1234,4/7/2014,5/7/2014
656666666666666ABC1234,5/7/2014,6/8/2014
656666666666666ABC1234,6/8/2014,7/8/2014
656666666666666ABC1234,7/8/2014,8/7/2014
656666666666666ABC1234,8/7/2014,9/8/2014
656666666666666ABC1234,9/8/2014,10/7/2014
656666666666666ABC1234,10/7/2014,11/9/2014
656666666666666ABC1234,11/9/2014,12/7/2014
777777777777777XYZ1234,5/7/2014,6/8/2014
777777777777777XYZ1234,6/8/2014,7/8/2014
777777777777777XYZ1234,7/8/2014,8/7/2014
777777777777777XYZ1234,8/7/2014,9/8/2014
777777777777777XYZ1234,9/8/2014,10/7/2014
777777777777777XYZ1234,10/7/2014,11/9/2014
777777777777777XYZ1234,11/9/2014,12/7/2014
您必须创建类似于 DESTINATION_TABLE 列的数据表列,然后进行列映射
DataTable datatable;
datatable = new DataTable("temptable");
datatable.Columns.Add("TBL_COL1 ");
datatable.Columns.Add("TBL_COL2 ");
datatable.Columns.Add("TBL_COL3");
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
copytothetable .DestinationTableName = "DESTINATION_TABLE";
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in datatable.Columns)
{
if (row[col] != DBNull.Value)
{
copytothetable.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
}
}
copytothetable.WriteToServer(datatable);
}
您可以试试oracle transaction commit。也许这对你有帮助。
using (OracleConnection connectiontodb = new OracleConnection(databaseconnectionstring))
{
connectiontodb.Open();
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
OracleTransaction tran = connectiontodb.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
copytothetable.ColumnMappings.Add("TBL_COL1", "TBL_COL1");
copytothetable.ColumnMappings.Add("TBL_COL2", "TBL_COL2");
copytothetable.ColumnMappings.Add("TBL_COL3", "TBL_COL3");
copytothetable.DestinationTableName = "DESTINATION_TABLE";
copytothetable.WriteToServer(datatable);
tran.Commit();
}
catch
{
tran.Roolback();
}
}
}
我正在使用 OracleBlukCopy 将一个巨大的 CSV 文件转储到数据库中 table。这是我正在使用的代码。当我 运行 时,我收到一条错误消息,提示无法将 NULL 插入 TBL_COL1,但我的 CSV 具有 TBL_COL1.
的值经过研究,我得出结论,当 TBL_COL1 中有字符时会出错。知道为什么吗?
DataTable datatable = TranslateCSVFileToDataTable();
using (OracleConnection connectiontodb = new OracleConnection(databaseconnectionstring))
{
connectiontodb.Open();
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
copytothetable.ColumnMappings.Add("TBL_COL1", "TBL_COL1");
copytothetable.ColumnMappings.Add("TBL_COL2", "TBL_COL2");
copytothetable.ColumnMappings.Add("TBL_COL3", "TBL_COL3");
copytothetable.DestinationTableName = "DESTINATION_TABLE";
copytothetable.WriteToServer(datatable);
}
}
CREATE TABLE DESTINATION_TABLE
(
TBL_COL1 VARCHAR2(32) not null,
TBL_COL2 DATE not null,
TBL_COL3 DATE not null,
)
98987987987987987,6/23/2014,7/23/2014
98987987987987987,7/23/2014,8/21/2014
98987987987987987,8/21/2014,9/22/2014
98987987987987987,9/22/2014,10/21/2014
98987987987987987,10/21/2014,11/20/2014
98987987987987987,11/20/2014,12/22/2014
656666666666666ABC1234,1/8/2014,1/9/2014
656666666666666ABC1234,1/9/2014,2/9/2014
656666666666666ABC1234,2/9/2014,3/9/2014
656666666666666ABC1234,3/9/2014,4/7/2014
656666666666666ABC1234,4/7/2014,5/7/2014
656666666666666ABC1234,5/7/2014,6/8/2014
656666666666666ABC1234,6/8/2014,7/8/2014
656666666666666ABC1234,7/8/2014,8/7/2014
656666666666666ABC1234,8/7/2014,9/8/2014
656666666666666ABC1234,9/8/2014,10/7/2014
656666666666666ABC1234,10/7/2014,11/9/2014
656666666666666ABC1234,11/9/2014,12/7/2014
777777777777777XYZ1234,5/7/2014,6/8/2014
777777777777777XYZ1234,6/8/2014,7/8/2014
777777777777777XYZ1234,7/8/2014,8/7/2014
777777777777777XYZ1234,8/7/2014,9/8/2014
777777777777777XYZ1234,9/8/2014,10/7/2014
777777777777777XYZ1234,10/7/2014,11/9/2014
777777777777777XYZ1234,11/9/2014,12/7/2014
您必须创建类似于 DESTINATION_TABLE 列的数据表列,然后进行列映射
DataTable datatable;
datatable = new DataTable("temptable");
datatable.Columns.Add("TBL_COL1 ");
datatable.Columns.Add("TBL_COL2 ");
datatable.Columns.Add("TBL_COL3");
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
copytothetable .DestinationTableName = "DESTINATION_TABLE";
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in datatable.Columns)
{
if (row[col] != DBNull.Value)
{
copytothetable.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
}
}
copytothetable.WriteToServer(datatable);
}
您可以试试oracle transaction commit。也许这对你有帮助。
using (OracleConnection connectiontodb = new OracleConnection(databaseconnectionstring))
{
connectiontodb.Open();
using (OracleBulkCopy copytothetable = new OracleBulkCopy(connectiontodb))
{
OracleTransaction tran = connectiontodb.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
copytothetable.ColumnMappings.Add("TBL_COL1", "TBL_COL1");
copytothetable.ColumnMappings.Add("TBL_COL2", "TBL_COL2");
copytothetable.ColumnMappings.Add("TBL_COL3", "TBL_COL3");
copytothetable.DestinationTableName = "DESTINATION_TABLE";
copytothetable.WriteToServer(datatable);
tran.Commit();
}
catch
{
tran.Roolback();
}
}
}