1

I am using OracleBlukCopy to dump a huge CSV file into the database table. Here is the code I am using. When I run this I am getting an error saying cannot insert NULL into TBL_COL1, but my CSV has values for TBL_COL1.

After research I came to conclusion that it is giving an error when TBL_COL1 has characters in it. Any idea why?

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
4
  • Might you have an empty line in your csv? Commented Jan 15, 2015 at 16:21
  • did you try debugging your datatable...check to see if you datatable has any values. Commented Jan 15, 2015 at 16:28
  • 1
    @Politank-Z TRAILING NULLCOLS in the control file Commented Jan 15, 2015 at 16:43
  • @prasy datatable has got values Commented Jan 15, 2015 at 16:44

2 Answers 2

2

You can try oracle transaction commit. Maybe this helps to you.

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();
          }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You have to create the Datatable columns as similar to DESTINATION_TABLE columns and then do the column mappings

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.