【发布时间】:2011-07-29 18:20:17
【问题描述】:
我正在尝试使用 linq 将图像保存到 sql-server。我将字段类型设置为图像,在我的应用程序中,我使用文件上传控制器将图像文件读取为字节 []。问题来了,当我评估图像字段时,它无法将 byte[] 转换为二进制。你认为这是一种保存图像的好方法吗?
我的代码:
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 美分,你应该保存路径而不是图像。保存图像可能会导致您的数据库很快变得非常大并降低性能。
标签: c# sql-server linq