Exception and memory Issue in SQL Bulk Copy

sqlbulkcopy sql-server

Question

We are using SQL bulk copy to insert nearly 10 million rows into a SQL Server table.

We received this exception after 7400000 insertions:

OLE DB provider 'STREAM' for linked server '(null)' returned invalid data for column '[!BulkInsert].Amount'.

Please let us know if this can be resolved.

There is also memory leak problem.

Below is our code :

try
{
            using (SqlConnection SQlConn = new SqlConnection(Common.SQLConnectionString))
            {                   
                DataTable dt1 = FillEmptyDateFields(dtDestination);

                //SqlTableCreator ObjTbl = new SqlTableCreator(SQlConn);

                //ObjTbl.DestinationTableName = DestinationTable;

                using (System.Data.SqlClient.SqlBulkCopy bulkCopy = new System.Data.SqlClient.SqlBulkCopy(SQlConn,SqlBulkCopyOptions.TableLock,null))
                {

                    //bulkCopy = new System.Data.SqlClient.SqlBulkCopy(SQlConn);
                    bulkCopy.DestinationTableName = DestinationTable;
                    bulkCopy.BulkCopyTimeout = 0;
                    bulkCopy.BatchSize = dt1.Rows.Count; // Batch Size Added.
                    Logger.Log("DATATABLE FINAL :" + dt1.Rows.Count.ToString(), Logger.LogType.Info);
                    if (SQlConn.State == ConnectionState.Closed || SQlConn.State == ConnectionState.Broken)
                        SQlConn.Open();
                    bulkCopy.WriteToServer(dt1);
                    SQlConn.Close();
                    SQlConn.Dispose();
                    bulkCopy.Close();
                    if (bulkCopy != null)
                    {
                        ((IDisposable)bulkCopy).Dispose();
                    }                        
                }
                dt1.Dispose(); 
                dt1 = null;                           
            }

            dtDestination.Dispose();
            System.GC.Collect();
            dtDestination = null;                

}
catch (Exception ex)
{
    Logger.Log(ex, Logger.LogType.Error);
    throw ex;
}

Popular Answer

Maybe because you're trying to insert a NaN value into a float field?

I had this problem recently, and to figure out what was wrong, I just inserted everything into a table where the data type was NVARCHAR(MAX) and then I noticed that one value was NaN.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why