【问题标题】:How to insert multiple images into MySQL database table with foreign key referencing a single primary key如何使用引用单个主键的外键将多个图像插入 MySQL 数据库表
【发布时间】: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, CONSTRAINT propertyimage_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();
                            }
                        }
                    }

                }
            }

【问题讨论】:

    标签: c# mysql sql asp.net


    【解决方案1】:

    你的问题是这一行:

    using (MySqlCommand cmd = new MySqlCommand("INSERT INTO propertyimage(MultipleImageName, MultipleImageMap, PropertyID) VALUES (@MultipleImageName, @MultipleImageMap, LAST_INSERT_ID()); "))
    

    这将在第一次插入时起作用,因为 LAST_INSERT_ID 是适当的外键值。

    但在第二次插入时,LAST_INSERT_ID 现在已更改为您刚刚插入的记录的 ID 值(第一次插入)。

    要解决此问题,您需要获取 LAST_INSERT_IDinto a C# variable,然后将其传递到每个后续 SQL 语句中(即 @ForeignKeyID 而不是 LAST_INSERT_ID)。

    这意味着改变你的首先

    cmd.ExecuteNonQuery();
    

    到:

    cmd.ExecuteNonQuery();
    insertedID = cmd.LastInsertedId;
    

    insertedID 是您在方法顶部声明的变量(可能是int)。

    然后你需要改变:

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

    到:

    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("@MultipleImageMap", "ImagesUploaded/" + file.FileName);
            cmd.Parameters.AddWithValue("@InsertedID", InsertedID);
    
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    

    【讨论】:

    • 嗨@mjwills,感谢您的快速回复。我理解你的意思,但我仍然对将链接代码放在我的方法中的位置感到有些困惑。
    • 已更新 - 有帮助吗?
    • 完美运行,非常感谢,我差点放弃了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2012-07-15
    相关资源
    最近更新 更多