【发布时间】:2017-12-14 05:05:49
【问题描述】:
我有一个 MYSQL 数据库,其中包含两个表,property 和 propertyImages,property 有一个主键(propertyID),它与其他列一起自动递增,propertyImages 有一个外键,即 propertyID。
当我在属性表中插入数据时,例如属性名称、位置等,用户选择的多个图像被插入到属性图像表中,并且外键与属性表中最后插入的主键相同。
当我插入数据时,属性表行被正确填充,没有问题,并且选择的多个图像的第一个图像使用正确的外键输入到 propertyImages 表中,但随后它崩溃并且不保存其余图像或图片路径。
string constr = ConfigurationManager.ConnectionStrings["realestatedbAddConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO property (PropertyName, PropertyNumBeds, PropertyType, PropertyPrice, PropertyFeatures, PropertyLocation, PropertyInformation, ImageName, ImageMap) VALUES (@PropertyName, @PropertyNumBeds, @PropertyType, @PropertyPrice, @PropertyFeatures, @PropertyLocation, @PropertyInformation, @ImageName, @ImageMap)"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Parameters.AddWithValue("@PropertyName", PropertyName);
cmd.Parameters.AddWithValue("@PropertyNumBeds", PropertyNumBeds);
cmd.Parameters.AddWithValue("@PropertyPrice", PropertyPrice);
cmd.Parameters.AddWithValue("@PropertyType", PropertyType);
cmd.Parameters.AddWithValue("@PropertyFeatures", PropertyFeatures);
cmd.Parameters.AddWithValue("@PropertyLocation", PropertyLocation);
cmd.Parameters.AddWithValue("@PropertyInformation", PropertyInformation);
string FileName = Path.GetFileName(MainImageUploada.FileName);
MainImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + FileName);
cmd.Parameters.AddWithValue("@ImageName", FileName);
cmd.Parameters.AddWithValue("@ImageMap", "ImagesUploaded/" + FileName);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
if (ImageUploada.HasFiles)
{
foreach(var file in ImageUploada.PostedFiles)
{
string FileName = Path.GetFileName(ImageUploada.FileName);
ImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName);
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO propertyimage(MultipleImageName, MultipleImageMap, PropertyID) VALUES (@MultipleImageName, @MultipleImageMap, LAST_INSERT_ID()); "))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Parameters.AddWithValue("@MultipleImageName", file.FileName);
cmd.Parameters.AddWithValue("@MultipleImageMap", "ImagesUploaded/" + file.FileName);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
txtName.Text = "";
txtPropFeat.Text = "";
txtPropInfo.Text = "";
txtPropLoc.Text = "";
txtNumBeds.Text = "";
txtPrice.Text = "";
txtPropType.Text = "";
Label1.Visible = true;
Label1.Text = "Property Added to Database Successfully!";
}
这是错误信息: MySql.Data.dll 中出现“MySql.Data.MySqlClient.MySqlException”类型的异常,但未在用户代码中处理
附加信息:无法添加或更新子行:外国 键约束失败 (
realestatedb.propertyimage, CONSTRAINTpropertyimage_ibfk_1外键 (PropertyID) 参考property(PropertyID) ON DELETE CASCADE ON UPDATE CASCADE)
编辑:有类似问题的人的工作示例
string PropertyName = txtName.Text;
string PropertyFeatures = txtPropFeat.Text;
string PropertyLocation = txtPropLoc.Text;
string PropertyInformation = txtPropInfo.Text;
string PropertyNumBeds = txtNumBeds.Text;
string PropertyPrice = txtPrice.Text;
string PropertyType = txtPropType.Text;
long InsertedID;
string constr = ConfigurationManager.ConnectionStrings["realestatedbAddConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO property (PropertyName, PropertyNumBeds, PropertyType, PropertyPrice, PropertyFeatures, PropertyLocation, PropertyInformation, ImageName, ImageMap) VALUES (@PropertyName, @PropertyNumBeds, @PropertyType, @PropertyPrice, @PropertyFeatures, @PropertyLocation, @PropertyInformation, @ImageName, @ImageMap)"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Parameters.AddWithValue("@PropertyName", PropertyName);
cmd.Parameters.AddWithValue("@PropertyNumBeds", PropertyNumBeds);
cmd.Parameters.AddWithValue("@PropertyPrice", PropertyPrice);
cmd.Parameters.AddWithValue("@PropertyType", PropertyType);
cmd.Parameters.AddWithValue("@PropertyFeatures", PropertyFeatures);
cmd.Parameters.AddWithValue("@PropertyLocation", PropertyLocation);
cmd.Parameters.AddWithValue("@PropertyInformation", PropertyInformation);
string FileName = Path.GetFileName(MainImageUploada.FileName);
MainImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + FileName);
cmd.Parameters.AddWithValue("@ImageName", FileName);
cmd.Parameters.AddWithValue("@ImageMap", "ImagesUploaded/" + FileName);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
InsertedID = cmd.LastInsertedId;
con.Close();
}
}
}
if (ImageUploada.HasFiles)
{
foreach(var file in ImageUploada.PostedFiles)
{
string FileName = Path.GetFileName(ImageUploada.FileName);
ImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName);
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO propertyimage(MultipleImageName, MultipleImageMap, PropertyID) VALUES (@MultipleImageName, @MultipleImageMap, @InsertedID); "))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Parameters.AddWithValue("@MultipleImageName", file.FileName);
cmd.Parameters.AddWithValue("@MultipleImageMap", "ImagesUploaded/" + file.FileName);
cmd.Parameters.AddWithValue("@InsertedID", InsertedID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
【问题讨论】: