如何使用LINQ保存图像到SQL服务器? [英] How to save image into sql-server using linq?
本文介绍了如何使用LINQ保存图像到SQL服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用LINQ保存图像到SQL服务器。我设置了字段类型是形象,在我的应用程序使用一个FileUpload CONTROLER读取图像文件的byte []。问题来了,就不能转换的byte []为二进制,当我评价图像领域。 ?你认为这是保存图像的好办法
我的代码:
流imgStream = FileUpload1.PostedFile.InputStream;
INT imglen = FileUpload1.PostedFile.ContentLength;
串imgContentType = FileUpload1.PostedFile.ContentType;
串imgName = FileUpload1.PostedFile.FileName;
字节[] = imgBinaryData新的字节[imglen]
INT N = imgStream.Read(imgBinaryData,0,imglen);
ImgStore信息=新ImgStore();
info.pic = imgBinaryData; //不能implictly转换型字节[]'来Linq.Binary
info.type = imgContentType;
info.fileName = imgName;
解决方案
的使用LINQ中的图像储存到SQL:转换字节数组或流式传输到二进制
字节数组转换为二进制LINQ,你可以利用二进制构造:
MyObject.BinaryProperty =新的二进制(字节);
参考
I'm trying to save image into sql-server using linq. I set the field type to be image, in my application I use a fileupload controler to read the image file as byte[]. Here comes the question, it cannot convert byte[] to binary when I evaluate the image field. Do you think it's a good way to save image?
My code:
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;
解决方案
Storing images with LINQ to SQL: Converting byte array or stream to Binary
To convert a byte array to a LINQ Binary, you can utilize the Binary constructor:
MyObject.BinaryProperty = new Binary(bytes);
References
这篇关于如何使用LINQ保存图像到SQL服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文