2013-07-06 3 views
1

SQL 테이블에서 이미지를 검색하여 목록에 저장하고이를 내 listview에 표시하려고합니다. 그러나 나는 이것을 어떻게하는지 모른다. 도움이 필요해.데이터베이스의 이미지로 목록 채우기

SQL Server 2008, Visual Studio 2008, C# Window 응용 프로그램을 사용하고 있습니다. 여기

내 코드입니다 :

cmd = new SqlCommand("Select ScanImage from ScanDocuments", con); 
dr = cmd.ExecuteReader(); 

List<ImageList> lstitem = new List<ImageList>(); 

while (dr.Read()) 
{ 
    ImageList _image = new ImageList(); 
    byte[] data = (byte[])dr["ScanImage"]; 

    MemoryStream ms = new MemoryStream(data); 
    Image bmp = new Bitmap(ms); 

    lstitem.Add(bmp); 
} 
+2

여러분의 노력을 보여주세요! 지금까지 뭐 해봤 어? 어디서 붙어 있니? 당신이 겪고있는 문제는 무엇입니까 ?? –

+0

친절하게 내 편집 된 질문보기. lstitem.add (bmp)에서 오류가 발생했습니다. –

답변

1

코드는 몇 가지 결함이있다 -이 대신 같은 것을 사용할 필요가 : - 그것은 101 ThumbNailPhoto를로드

// define connection string and select statement 
// I used AdventureWorks 2012 database - change to match *YOUR* environment 
string connectionString = "server=.;database=AdventureWorks2012;integrated security=SSPI;"; 
string query = "SELECT ThumbNailPhoto FROM Production.ProductPhoto"; 

// define a list of "Image" objects 
List<Image> listOfImages = new List<Image>(); 

// using the SqlConnection and SqlCommand .... 
using(SqlConnection conn = new SqlConnection(connectionString)) 
using (SqlCommand selectCmd = new SqlCommand(query, conn)) 
{ 
    // open connection 
    conn.Open(); 

    // execute SqlCommand to return a SqlDataReader 
    using (SqlDataReader rdr = selectCmd.ExecuteReader()) 
    { 
     // iterate over the reader 
     while (rdr.Read()) 
     { 
       // load the bytes from the database that represent your image 
       var imageBytes = (byte[]) rdr[0]; 

       // put those bytes into a memory stream and "rewind" the memory stream 
       MemoryStream memStm = new MemoryStream(imageBytes); 
       memStm.Seek(0, SeekOrigin.Begin); 

       // create an "Image" from that memory stream 
       Image image = Image.FromStream(memStm); 

       // add image to list 
       listOfImages.Add(image); 
     } 
    } 

    conn.Close(); 
} 

이 나를 위해 잘 작동합니다 AdventureWorks2012 데이터베이스

관련 문제