将图像从URL插入数据库

时间:2011-05-26 23:17:56

标签: asp.net sql-server

抱歉这个简单易懂的问题 我试图将网址中的图片插入数据库。这里需要插入alb.picture。

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

1 个答案:

答案 0 :(得分:2)

您无需将文件存储在本地硬盘上。将图像读入MemoryStream并将二进制数据直接写入数据库更快更可靠。