Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precision Lost while Bulk Inserting records into SQL using SqlBulkCopy

I am using the following code to Bulk Insert a data table into my SQL Table:

 // Set up the bulk copy object.  
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(destinationConnection.Connection))
                {
                    bulkCopy.DestinationTableName =
                        Constants.ReportDataTable;

                    // Write from the source to the destination.
                    DataTable dtBulk = GetDatatableInReportDataFormat(dt, objectName, version);
                    bulkCopy.WriteToServer(dtBulk);//To get the Datatable in the SQL table format

                }

I have a column in my SQL Table named "Value", its type is decimal (28,5). My problem is that some values with decimal numbers are being automaticaly rounded, thus I am losing precison, for example a value of 0.72768 is being saved as 0.72767.

In the Datatable, the column "Value" is of type Double.

Any body has an idea? Thank

like image 841
Hassan Mokdad Avatar asked Jun 30 '13 14:06

Hassan Mokdad


1 Answers

Make the column in the DataTable as decimal rather than double… I strongly suspect this will make the problem disappear.

like image 194
Marc Gravell Avatar answered Sep 27 '22 20:09

Marc Gravell