图像不保存在数据库中

问题描述:

我在图像数据库中插入图像。问题是当我点击上传插入数据库的所有记录,除了图像。该代码不会产生任何错误。表格中的图像列是空白的。图像不保存在数据库中

在此先感谢。

这是我的代码:

 int length = FileUpload1.PostedFile.ContentLength; 
     byte[] pic = new byte[length]; 
     FileUpload1.PostedFile.InputStream.Read(pic, 0, length); 

     try 
     { 
      SqlConnection con = new SqlConnection(conn); 
      con.Open(); 
      //inserting uploaded image query 
      SqlCommand com = new SqlCommand("INSERT INTO upload (ID,I_WANT_TO,PROPERTY_TYPE,PROPERTY_TYPE1,CITY,LOCALITY,SOCIETY_OR_PROJECT_NAME,BEDROOM,BATHROOM,BALCONY,SUPER_BUILD_UP_AREA,SUPER_BUILD_UP_AREA_1,BUILD_UP_AREA,BUILD_UP_AREA_1,CARPET_AREA,CARPET_AREA_1,EXPECTED_PRICE,PROPERTY_ON_FLOOR,TOTAL_FLOOR_IN_BUILDING,TRANSACTION_TYPE,PROPERTY_OWNERSHIP,AVAILABILITY,AGE_OF_PROPERTY,POSSESSION_OF_PROPERTY,IMAGE_1,PROPERTY_DESCRIPTION,FULL_NAME,EMAIL,PASS,CONTACT) VALUES (@ID,@I_want_to,@Type_of_property,@type_of_property_1,@cities,@locality,@society_or_project_name,@bedroom,@bathroom,@balcony,@Super_Build_up_Area,@Super_build_up_area_1,@Build_up_area,@Build_up_area_1,@Carpet_area,@Carpet_area_1,@Expected_Price,@Property_on_Floor,@Total_Floor_in_Building,@Transaction_Type,@Property_Ownership,@Availability,@Age_of_Property,@Possession_of_Property,@image_1,@Property_Description,@fullname,@Email,@Pass,@contact)", con); 

      com.Parameters.AddWithValue("@ID", rand()).ToString(); 
      com.Parameters.AddWithValue("@I_want_to", I_want_to.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@type_of_property", Type_of_property.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@type_of_property_1", type_of_property_1.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@cities", cities.Text).ToString(); 
      com.Parameters.AddWithValue("@locality", locality.Text).ToString(); 
      com.Parameters.AddWithValue("@society_or_project_name", society_or_project_name.Text).ToString(); 
      com.Parameters.AddWithValue("@bedroom", bedroom.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@bathroom", bathroom.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@balcony", balcony.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Super_Build_up_Area", Super_Build_up_Area.Text).ToString(); 
      com.Parameters.AddWithValue("@Super_Build_up_Area_1", Super_build_up_area_1.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@build_up_area", Build_up_area.Text).ToString(); 
      com.Parameters.AddWithValue("@build_up_area_1", Build_up_area_1.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@carpet_area", Carpet_area.Text).ToString(); 
      com.Parameters.AddWithValue("@carpet_area_1", Carpet_area_1.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@expected_price", Expected_Price.Text).ToString(); 
      com.Parameters.AddWithValue("@property_on_floor", Property_on_Floor.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@total_floor_in_building", Total_Floor_in_Building.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Transaction_Type", Transaction_Type.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Property_Ownership", Property_Ownership.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Availability", Availability.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Age_of_Property", Age_of_Property.SelectedValue).ToString(); 
      com.Parameters.AddWithValue("@Possession_of_Property", Possession_of_Property.Text).ToString(); 
      com.Parameters.AddWithValue("@image_1", pic); 
      com.Parameters.AddWithValue("@Property_Description", Property_Description.Text).ToString(); 
      com.Parameters.AddWithValue("@fullname", fullname.Text).ToString(); 
      com.Parameters.AddWithValue("@email", email.Text).ToString(); 
      com.Parameters.AddWithValue("@pass", password.Text).ToString(); 
      com.Parameters.AddWithValue("@contact", contact.Text).ToString(); 

      com.ExecuteNonQuery(); 

      Response.Write("<script>alert('congratulations, You have successfully upload property details');</script>"); 
      Response.Redirect("Index.aspx"); 
      com.Dispose(); 
     } 
     finally 
     { 
      con.Close(); 
     } 
+0

哇!请尝试做一个记录,然后看看结果如何。我相信这将是一个更清晰和有价值的问题。 –

停止这样做。不要将图像存储在数据库中。我不记得有多少人停止这样做。将文件保存到一个目录,并将其作为一个字符串存储到数据库中,并且没有其他任何内容触及它。

+0

这不是我所期望的......我会记住下一次..现在请帮忙​​ –

+0

好的。你可以随时把它存储为一个字节数组(可以读作字符串)。 byte [] bytes = System.IO.File.ReadAllBytes(filename); 我不会推荐这样做。 – user853710