I am using SqlBulkCopy.WriteToServer
-method to insert bulk data to database table. When I am passing a datatable as the parameter of the method WriteToServer
, some numeric values (numeric(17,6))
is getting subtracted by .000001
while it is inserted in database table. But not all values are getting changed, it is happening randomly, couldn't identify the pattern. eg- 10068.121
is being changed to 10068.120999
Now when I am passing a datareader as the parameter of the method WriteToServer, it is working fine, the numeric values remain unchanged.
Any idea why it is happening for datatable and not for datareader? My application is a C# console application.
Here is my code… if I pass the datatable (_dt) to _sqlBulkCopy.WriteToServer method, then I have the issue. But when I convert the datatable (_dt) to DataTableReader (_Reader), it is working fine.
DataTable _dt = new DataTable();
_odbCDA.Fill(_dt);
DataTableReader _Reader = new DataTableReader(_dt);
using (SqlBulkCopy _sqlBulkCopy = new SqlBulkCopy(_sqlConnectionString))
{
_sqlBulkCopy.BulkCopyTimeout = 3600;
_sqlBulkCopy.DestinationTableName = _tableName;
_sqlBulkCopy.WriteToServer(_Reader); //This works
//_sqlBulkCopy.WriteToServer(dt); //This is having the issue
_sqlBulkCopy.Close();
}
It's hard to be certain without an example reproduction, but if I had to guess when you're using DataReader
you're copying data between two tables that have the same definition/precision for the numeric type, but when using DataTable
the column does not have matching precision. Presumably this is because the DataReader
isn't mapping things into .NET types, but the DataTable
is.
I think (but have not tested) .NET types map about like so (at least in SQL Server):
float
-> SQL real
double
-> SQL float
decimal
-> SQL money
or numeric
SQL let's you specify precision in ways C# doesn't, so if you're working with C# built-in types there's a solid chance you're performing conversions to and from numeric(17,6)
which could lose precision.
If my guess is correct, the DataType
on the affected DataColumn
in the DataTable
will be something like System.Double
, System.Single
, or System.Decimal
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.