[英]Error while trying to save an image into SQL Server
我已經創建了將圖像上傳到SQL Server的代碼。
以下是將圖像轉換為字節的代碼:
//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(p);
//Open FileStream to read file
FileStream fStream = new FileStream(p, FileMode.Open, FileAccess.Read);
byte[] numBytes = new byte[fStream.Length];
fStream.Read(numBytes, 0, Convert.ToInt32(fStream.Length));
//Use BinaryReader to read file stream into byte array.
//BinaryReader br = new BinaryReader(fStream);
//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.
// data = br.ReadBytes((int)numBytes);
return numBytes;
以下是將字節作為值添加到SqlCommand
參數的代碼:
objCmd.Parameters.Add("@bill_Image", SqlDbType.Binary).Value = imageData;
objCmd.ExecuteNonQuery();
但我收到了錯誤
字符串或二進制數據將被截斷。 該語句已終止
我怎樣才能克服這個問題?
錯誤清楚地表明您正在嘗試保存比字段定義允許的更多字節。
不確定你用於bill_Image
sql類型,但是用於存儲圖像的適當字段定義將是varbinary(MAX)
。
檢查數據庫中bill_Image
列的定義。
它應該是類似的東西
bill_Image varbinary(X)
只需增加X或放置MAX而不是數字(如果你有超過8 000字節的圖像)
有關binary / varbinary類型的信息,請點擊此處
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.