2016-09-28 48 views
0

我想从我的数据库表中获取Image列的值。使用C#实体框架从SQL数据库获取图像类型

我以前使用过这个代码,但现在我想调用一个函数从另一个表中查询它。

MemoryStream ms = new MemoryStream(obj.photo, 0, obj.photo.Length); 
ms.Position = 0; // this is important   
pbFarmer.Image = Image.FromStream(ms, true); 

此代码抛出一个错误:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Drawing.Image'. An explicit conversion exists

public Image getphoto(int id) 
{ 
    using (simisdbEntities db = new simisdbEntities()) 
    { 
     // return db.FarmerImages.Where(u => u.id == id).Select(u => u.photo); 
     // return db.FarmerImages.SqlQuery("SELECT photo FROM dbo.FarmersImages where id =" + id); 

     // return db.FarmerImages.First(a => a.id == id); 

     var img = from p in db.FarmerImages 
        where p.id == id 
        select Image.FromStream(new MemoryStream(p.photo.ToArray())); 
     return img; 
    } 
} 
+0

既然你已经有了这个ID,这个工作吗? var img = db.FarmerImage.Find(id)。'propertyname'... propertyname将是找到的实体的属性。 – nocturns2

回答

1

的问题是,你的LINQ查询返回的IQueryable<Image>而不是单个Image对象,所以你不能从你的方法返回返回它只有一个图像。你需要以某种方式获得唯一的价值。

你可以像这样的东西去:

using (simisdbEntities db = new simisdbEntities()) 
{ 
    IQueryable<Image> img = from p in db.FarmerImages 
          where p.id == id 
          select Image.FromStream(new MemoryStream(p.photo.ToArray())); 
    return img.FirstOrDefault(); 
} 

但后来你会得到一个运行时异常:

Additional information: LINQ to Entities does not recognize the method 'System.Drawing.Image FromStream(System.IO.Stream)' method, and this method cannot be translated into a store expression.

我第一次从数据库中获取的FarmerImage元,处理可能的例外 - 例如。不存在图像 - 然后返回Image对象:

UPDATE: After your comments below, I'd suggest you to also check for the case when you image exists in database, but the photo array is null.

public Image getphoto(int id) 
{ 
    using (var db = new simisdbEntities()) 
    { 
     var imgMetadata = db 
       .FarmerImages 
       .FirstOrDefault(p => p.id == id); 

     //handle the case when image does not exist. 
     if (imgMetadata == null) 
      throw new Exception("Image not found!"); 

     //update: check for the case when a FarmerImage exists in database but the photo array is null 
     if (image.photo == null) 
      throw new Exception("Image not found!"); 

     //read the image bytes into an Image object. 
     var img = Image.FromStream(new MemoryStream(imgMetadata.photo.ToArray())); 

     return img; 
    } 
} 

请记住,你可以调用的方法和处理异常 - 我会做这种方式 - 或者你可以空,而不是返回抛一个异常和处理返回的图像为空时的情况。

First case: expecting an exception

public void CallerHanderOrMethod() 
{ 
    try{ 
     var img = farmerManager.getPhoto(farmerId); 
    } 
    catch(Exception ex) //consider throwing a more specific exception. 
    { 
     //load the default silhouette image into the picture box. 
    } 
} 

Second case: expecting a null image.

public void CallerHanderOrMethod() 
{ 
    var img = farmerManager.getPhoto(farmerId); 
    if (img == null) 
    { 
     //load the default silhouette image into the picture box. 
    } 
} 

这应该做的工作。

希望这会有所帮助!

+0

感谢您花时间帮助我。 – ivias

+0

嗨在我用来填写图片框之前,如果没有像这样的图像:pbFarmer.Image = Properties.Resources.male_silhouette;如何在getphoto函数中添加如果没有找到图像我想显示这个默认 – ivias

+0

@ivias,很高兴帮助!你可以从你的方法返回null,而不是抛出异常,并处理你调用getphoto()的情况,所以如果图像为null,你可以加载你的默认轮廓。此外,你应该检查你的方法,不仅如果图像不存在,而且如果mage.photo数组为null。我已经更新了答案。看一看! –

相关问题