简体   繁体   English

C# 中的 SqlBulkCopy class 正在更改数值

[英]SqlBulkCopy class in C# is changing a numeric value

I am using SqlBulkCopy.WriteToServer -method to insert bulk data to database table.我正在使用SqlBulkCopy.WriteToServer -方法将批量数据插入数据库表。 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.当我将数据表作为方法WriteToServer的参数传递时,一些数值(numeric(17,6))在插入数据库表时被减去.000001 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 10068.121正在更改为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.现在,当我将数据读取器作为方法 WriteToServer 的参数传递时,它工作正常,数值保持不变。

Any idea why it is happening for datatable and not for datareader?知道为什么它发生在数据表而不是数据读取器上吗? My application is a C# console application.我的应用程序是 C# 控制台应用程序。

Here is my code… if I pass the datatable (_dt) to _sqlBulkCopy.WriteToServer method, then I have the issue.这是我的代码……如果我将数据表 (_dt) 传递给 _sqlBulkCopy.WriteToServer 方法,那么我就有问题了。 But when I convert the datatable (_dt) to DataTableReader (_Reader), it is working fine.但是当我将数据表 (_dt) 转换为 DataTableReader (_Reader) 时,它工作正常。

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.没有示例复制很难确定,但是如果我不得不猜测您何时使用DataReader您正在两个具有相同数字类型定义/精度的表之间复制数据,但是当使用DataTable时,该列不会有匹配的精度。 Presumably this is because the DataReader isn't mapping things into .NET types, but the DataTable is.大概这是因为DataReader没有将事物映射到 .NET 类型,但DataTable是。

I think (but have not tested) .NET types map about like so (at least in SQL Server):认为(但尚未测试).NET 类型 map 大约是这样(至少在 SQL 服务器中):

  • C# float -> SQL real C# float -> SQL real
  • C# double -> SQL float C# double -> SQL float
  • C# decimal -> SQL money or numeric C# decimal -> SQL moneynumeric

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. SQL 让您以 C# 没有的方式指定精度,因此,如果您使用 C# 内置类型丢失精度,那么您很有可能执行从numeric(17,6)

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 .如果我的猜测是正确的, DataTable中受影响的DataColumn上的DataType将类似于System.DoubleSystem.SingleSystem.Decimal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM