我创建了将图像上传到 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();
但我收到错误
字符串或二进制数据将被截断。该语句已终止
我该如何克服这个问题?