繁体   English   中英

尝试将图像保存到SQL Server时出错

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM