2011-11-24 3 views
2

MySQL 데이터베이스에서 LONGBLOB을 검색하려고하지만 어떻게해야할지 모르겠다. 나는 interwebz를 수색했고, 정말로 도움이되는 (이해할 수있는) 아무것도 발견되지 않았다. LONGBLOB을 검색 할 때이를 이미지로 저장하려고합니다. C#의 MySQL에서 LONGBLOB 검색

내가 이미 뭘하려 : 사전에

 int bufferSize = 100; 
     byte[] bin = new byte[bufferSize]; 
     long retval = 0; 
     long startIndex = 0; 

     MemoryStream ms = null; 
     Image image = null; 

     MySqlCommand command = new MySqlCommand("select * from image where uid = @uid", Connection.Connect()); 
     command.Parameters.AddWithValue("@uid", "2"); 
     MySqlDataReader reader = command.ExecuteReader(); 

     if (reader.Read()) 
     { 
      retval = reader.GetBytes(reader.GetOrdinal("logo"), startIndex, bin, 0, bufferSize); 
     } 


     ms = new MemoryStream(bin); 
     image = Image.FromStream(ms); 

감사합니다.

답변

3

내가 작업 한 프로젝트의 일환으로 실제로 이런 짓을 ...

 
public Bitmap loadImage(int imgID) 
     { 

      MySqlDataReader myData; 
      MySqlCommand cmd = new MySqlCommand(); 

      string SQL; 
      byte[] rawData; 
      MemoryStream ms; 
      UInt32 FileSize; 
      Bitmap outImage; 

      SQL = "SELECT ImageName, ImageSize, Image FROM Images WHERE ImageID ="; 
      SQL += imgID.ToString(); 

      try 
      { 
       cmd.Connection = connection; 
       cmd.CommandText = SQL; 

       myData = cmd.ExecuteReader(); 

       if (!myData.HasRows) 
        throw new Exception("There are no blobs to save"); 

       myData.Read(); 

       FileSize = myData.GetUInt32(myData.GetOrdinal("ImageSize")); 
       rawData = new byte[FileSize]; 

       myData.GetBytes(myData.GetOrdinal("Image"), 0, rawData, 0, (Int32)FileSize); 


       ms = new MemoryStream(rawData); 
       outImage = new Bitmap(ms); 
       ms.Close(); 
       ms.Dispose(); 

       myData.Close(); 
       myData.Dispose(); 

       cmd.Dispose(); 

       return outImage; 


      } 
      catch (MySqlException ex) 
      { 
       MessageBox.Show(ex.Message); 
       return null; 
      } 

     } 

희망이 도움이됩니다. 또한 나쁜 코딩 방법을 변명 해주십시오.

감사 톰

관련 문제