[英]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.