简体   繁体   English

来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型

[英]The given value of type String from the data source cannot be converted to type varbinary of the specified target column

I have a datatable with one column which has image file path. 我有一个具有图像文件路径的列的数据表。 I need to replace the file path with byte array of that image. 我需要用该图像的字节数组替换文件路径。 The below code gives me error. 下面的代码给我错误。

The given value of type String from the data source cannot be converted to type varbinary of the specified target column. 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型。

for (int rowIncrement = 0; rowIncrement < dt.Rows.Count; rowIncrement++)
{
    byte[] photo = GetPhoto(dt.Rows[rowIncrement]["image"]);
    dt.Rows[rowIncrement]["image"] = photo;
    dt.AcceptChanges();
}
using (SqlBulkCopy copy = new SqlBulkCopy(sqlCon, SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.KeepIdentity, null))
{
    copy.DestinationTableName = "UserDetails";
    copy.WriteToServer(dt);
}

The column type should be specified otherwise the columns value is still a string when you need a byte[] 应该指定列类型,否则当需要byte []时,列值仍然是字符串

Example: 例:

// ADD a new column since you cannot change the type

dt.Columns.Add("image_tmp", typeof (byte[]));

for (int rowIncrement = 0; rowIncrement < dt.Rows.Count; rowIncrement++)
{
    byte[] photo = GetPhoto(dt.Rows[rowIncrement]["image"]);
    dt.Rows[rowIncrement]["image_tmp"] = photo;
    dt.AcceptChanges();
}

// REMOVE image column and rename the temporary column
dt.Columns.Remove("image");
dt.Columns["image_tmp"].ColumnName = "image";

暂无
暂无

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

相关问题 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' SQlBulkCopy 无法将数据源中 DateTime 类型的给定值转换为指定目标列的 int 类型 - SQlBulkCopy The given value of type DateTime from the data source cannot be converted to type int of the specified target column SqlBulkCopy引发异常:无法将数据源中String类型的给定值转换为指定目标列的bigint类型 - SqlBulkCopy throws exception: The given value of type String from the data source cannot be converted to type bigint of the specified target column SQL批量复制“使用ASP.NET无法将数据源中String类型的给定值转换为指定目标列的类型日期时间” - SQL Bulk Copy “The given value of type String from the data source cannot be converted to type datetime of the specified target column” using ASP.NET 无法在错误的varbinary(max)中插入空值:不允许从数据类型nvarchar隐式转换为varbinary(max) - Cannot insert null value in varbinary(max) with error : implicit conversion from data type nvarchar to varbinary(max) is not allowed 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM