图像未保存在数据库中
本文关键字:数据库 存在 保存 图像 | 更新日期: 2023-09-27 18:31:36
我在数据库中插入图像。问题是当我单击上传除图像之外插入数据库的所有记录时。代码未生成任何错误。表中的图像列为空。
提前谢谢。
这是我的代码:
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();
}
别这样了。不要将图像存储在数据库中。我不记得有多少人阻止了我这样做。您将文件保存到一个目录中,它的路径以字符串形式存储到数据库中,其他任何东西都不会触及它。