我试图用linq将图像保存到sql-server。我将字段类型设置为图像,在我的应用程序中,我使用fileupload控制器以byte []形式读取图像文件。问题在于,当我评估图像字段时,无法将byte []转换为二进制。你认为这是保存图像的好方法吗?如何使用linq将图像保存到sql-server?
我的代码:
Stream imgStream = FileUpload1.PostedFile.InputStream;
int imglen = FileUpload1.PostedFile.ContentLength;
string imgContentType = FileUpload1.PostedFile.ContentType;
string imgName = FileUpload1.PostedFile.FileName;
byte[] imgBinaryData = new byte[imglen];
int n = imgStream.Read(imgBinaryData, 0, imglen);
ImgStore info = new ImgStore();
info.pic = imgBinaryData;// it cannot implictly convert type 'byte[]' to Linq.Binary
info.type = imgContentType;
info.fileName = imgName;
我的2美分,你应该保存的路径不是图像。保存图像可能会导致您的数据库非常快速地变大并降低性能。 – Tony