2016-09-28 9 views
0

Imageの値をデータベーステーブルから取得したいとします。C#Entity Frameworkを使用して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クエリではなく、単一のImageオブジェクトのIQueryable<Image>を返しているということですイメージのみ。あなたは何とか1つの価値だけを得る必要があります。

あなたはこのような何かで行くことができます:

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(); 
} 

しかし、その後、あなたが実行時例外得るだろう:私は最初のデータベースからFarmerImageメタデータを取得したい

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.

を可能な例外を処理します。イメージが存在しない - と、その後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; 
    } 
} 

は、メソッドを呼び出すと例外を処理できることを覚えておいてください - 私はそれをこのようにしてください - またはあなたの代わりに投げるのnullを返すことができます戻り値のイメージがnullの場合は例外を処理します。

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、喜んで助けてください!あなたは例外をスローする代わりにあなたのメソッドからヌルを返すことができ、getphoto()を呼び出した場所を扱うことができるので、画像がヌルならデフォルトのシルエットをロードすることができます。また、イメージが終了しないだけでなく、mage.photo配列がnullの場合もメソッドをチェックインする必要があります。私は答えを更新しました。見てみましょう! –

関連する問題