SqlBulkCopy 与 Byte[] DataTable 列错误

发布于 2024-09-11 07:43:35 字数 510 浏览 2 评论 0原文

我有一个强类型数据集,其中包含一个数据表,其中一列作为 byte[] 列,我试图将其插入到二进制(4)数据库表字段中。我可以毫无问题地设置 byte[] 列值,但在数据表上运行 sqlbulkcopy 时收到以下异常:

“数据源中 Int32 类型的给定值无法转换为指定的二进制类型目标栏。”

数据表是一个大型数据表,sqlbulkcopy 可以很好地处理数据表和数据库表减去 byte[]/binary(4) 列。以下是我插入的使用 .NET 2.0 破坏 SqlBulkCopy 的代码。

byte[] codeByteArray = GetByteArray();
dt.byteArrayCol = codeByteArray;

...

using(SqlBulkCopy bc = new SqlBulkCopy(conn))
{
    bc.DestinationTableName = dt.TableName;
    bc.WriteToServer(dt);
    bc.Close();
}

I have a strongly typed dataset containing a datatable with one of the columns as a byte[] column that I'm trying to insert into a binary(4) database table field. I'm able to set the byte[] column values without any problems, but I receive the following exception when I run sqlbulkcopy on the datatable:

"The given value of type Int32 from the data source cannot be converted to type binary of the specified target column."

The datatable is a large datatable and the sqlbulkcopy works fine with the datatable and database table minus the byte[]/binary(4) columns. The following is the code that I've inserted that is breaking SqlBulkCopy using .NET 2.0.

byte[] codeByteArray = GetByteArray();
dt.byteArrayCol = codeByteArray;

...

using(SqlBulkCopy bc = new SqlBulkCopy(conn))
{
    bc.DestinationTableName = dt.TableName;
    bc.WriteToServer(dt);
    bc.Close();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-09-18 07:43:35

我发现了我的问题。我的数据表中的 Byte[] 列的序数位置与相应的数据库列序数不同。数据表列序数为 56,而数据库列序数为 8,因此需要重新组织数据表或 sqlbulkcopy 的列映射。重新组织数据表变得更加容易和快捷。

I discovered my problem. The Byte[] column in my datatable was not in the same ordinal position as the corresponding database column ordinal. The datatable column ordinal was 56 while the database column ordinal was 8, thus either the datatable needed to be re-organized or the columnmapping for the sqlbulkcopy. Reorganizing the datatable was much easier and quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文