2011-05-26 33 views
1

對不起,這個應該很容易的問題 我試圖從一個url插入圖片到數據庫中。這裏需要插入alb.picture。從url中插入圖片到數據庫

url = "https://graph.facebook.com/me/albums/?access_token=" + oAuth.Token; 
         json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty); 
         Albums albms = js.Deserialize<Albums>(json); 
         foreach (Album alb in albms.data) 
         { 
          alb.picture = "https://graph.facebook.com/" + alb.id + "/picture/?access_token=" + oAuth.Token; 

          FileStream fs; 
          string sfn = alb.picture; 

          //FileInfo filImage = new FileInfo(sfn); 
          m_lImageFileLength = alb.picture.Length; 
          m_barrImg = new Byte[Convert.ToInt32(m_lImageFileLength)]; 
       **fs = new FileStream(sfn, FileMode.Open, FileAccess.Read);** // this is where it gives me an error abt sfn. C:\win...\sfn path not found 

          fs.Read(m_barrImg, 0, System.Convert.ToInt32(m_lImageFileLength)); 
          insert_Facebook_Photos(Convert.ToInt64(ui.id), m_barrImg, alb.created_time.ToString(), alb.updated_time.ToString()); 
          fs.Close(); 


    private void insert_Facebook_Photos(Int64 FacebookUserID, byte[] picbyte, string created_time, string updated_time) 
      { 
       string conString = "Data Source=HOME-590392F5B5\\SQLEXPRESSR2;Initial Catalog=FMM;Integrated Security=True"; 
       SqlConnection sqlConnection1 = new SqlConnection(); 
       sqlConnection1.ConnectionString = conString; 
       SqlCommand sqlCommand1 = new SqlCommand(); 
       try 
       { 

       sqlConnection1.Open(); 
       //if (sqlCommand1.Parameters.Count == 0) 
       { 
        sqlCommand1.CommandText = "INSERT INTO Facebook_Photos(FacebookUserID,Photo,Photo_CreatedDate,Photo_UpdatedDate) values(@ID,@Picture,@createdDate,@UpdatedDate)"; 
        sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 64); 
        sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image); 
        sqlCommand1.Parameters.Add("@createdDate", System.Data.SqlDbType.VarChar, 50); 
        sqlCommand1.Parameters.Add("@UpdatedDate", System.Data.SqlDbType.VarChar, 50); 
       } 
       sqlCommand1.Parameters["@ID"].Value = FacebookUserId; 
       sqlCommand1.Parameters["@Picture"].Value = picbyte; 
       sqlCommand1.Parameters["@createdDate"].Value = created_time; 
       sqlCommand1.Parameters["@UpdatedDate"].Value = updated_time; 
       sqlCommand1.ExecuteNonQuery(); 


      } 

**fs = new FileStream(sfn, FileMode.Open, FileAccess.Read);** // this is where it gives me an error abt sfn. C:\win...\sfn path not found 
alb.picture is supposed to be inserted and How do I get the path of this image 


      catch (Exception ex) 
      { 
       throw ex; 

      } 
      finally 
      { 
       sqlConnection1.Close(); 

      } 

     } 

感謝 Smitha

回答

2

你並不需要存儲在本地硬盤的文件。將圖像讀入MemoryStream會更快更可靠,並將二進制數據直接寫入數據庫。

+0

是否有任何鏈接閱讀有關此..從來沒有這之前 – Smitha 2011-05-26 23:30:27

+0

你已經完成了這項工作。只需用'MemoryStream'替換'FileStream'即可。你可以在這裏閱讀它:http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx – Xaqron 2011-05-26 23:39:12