图像不保存在数据库中

时间:2016-01-24 15:27:26

标签: c# asp.net

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

提前致谢。

这是我的代码:

        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();
        }

1 个答案:

答案 0 :(得分:0)

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