簡體   English   中英

來自數據源的String類型的給定值不能轉換為指定目標列的varbinary類型

[英]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);
}

應該指定列類型,否則當需要byte []時,列值仍然是字符串

例:

// 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM