简体   繁体   English

如何在外键列上更新和插入值?

[英]How can I update and insert value on my Foreign Key Column?

I want to insert and update my table. 我想插入和更新我的表。 My query statement is only indicate primary key, item name, description, price and so on.. The problem is I have another column which is the FOREIGN KEY. 我的查询语句仅指示主键,项目名称,描述,价格等。问题是我还有另一列是FOREIGN KEY。 When I try to insert values to my item database table the error is occurred. 当我尝试向我的项目数据库表中插入值时,发生了错误。 It said that the column count doesn't match value count at row 1. And also how can I update the table if I have a column of FOREIGN KEY? 它说列数与第1行的值数不匹配。如果我有一列FOREIGN KEY,又如何更新表?

class Item
    {
        public int ItemID { get; set; }
        public string ItemName { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
        public string Stocks { get; set; }

        public int GenerateID()
        {
            int newID = 0;
            Connection connection = new Connection();
            string sql = "SELECT ItemID FROM tbl_Item ORDER BY ItemID DESC Limit 1";
            MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            conn.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                newID = dr.GetInt32("ItemID");
            }
            conn.Close();
            newID++;
            return newID;
        }

        public void UpdateRecord()
        {
            try
            {
                Connection connection = new Connection();
                string sql = "UPDATE Item SET ItemName=@itemName, Description=@desc, Price=@price, Stocks=@stocks WHERE ItemID=@itemId";
                MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@itemId", ItemID);
                cmd.Parameters.AddWithValue("@itemName", ItemName);
                cmd.Parameters.AddWithValue("@desc", Description);
                cmd.Parameters.AddWithValue("@price", Price);
                cmd.Parameters.AddWithValue("@stocks", Stocks);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Update Successfully", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occured: " + e, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void InsertRecord()
        {
            Connection connection = new Connection();
            try
            {
                string sql = "INSERT INTO tbl_Account VALUES(@itemId, @itemName, @desc, @price, @stocks)";
                MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@itemId", ItemID);
                cmd.Parameters.AddWithValue("@itemName", ItemName);
                cmd.Parameters.AddWithValue("@desc", Description);
                cmd.Parameters.AddWithValue("@price", Price);
                cmd.Parameters.AddWithValue("@stocks", Stocks);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                MessageBox.Show("Update Successfully", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occurred: " + e, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
//for button ADD
private void btnAddItem_Click(object sender, EventArgs e)
        {
            Item item = new Item();

            item.ItemID = item.GenerateID();
            item.ItemName = txtINameItem.Text;
            item.Description = txtDescriptionItem.Text;
            item.Price = txtPriceItem.Text;
            item.Stocks = textStocksItem.Text;

            item.InsertRecord();
        }

Your SQL code is wrong. 您的SQL代码错误。 You must pass valid Foreign Key value while inserting the record. 插入记录时必须传递有效的外键值。 Valid Foreign Key value is the value already existing in the Parent Table OR NULL. 有效外键值是父表中已经存在的值或NULL。 However, setting NULL to Foreign Key column is based on how you have created the Foreign Key on the Child Table. 但是,将NULL设置为“外键”列是基于在子表上创建外键的方式。

Try below code replacing @fkValue & FkValue with correct Foreign Key column order in the Child Table and value: 尝试使用以下代码在子表和值中用正确的外键列顺序替换@fkValue和FkValue:

                string sql = "INSERT INTO tbl_Account VALUES(@itemId, @fkValue, @itemName, @desc, @price, @stocks)";
                MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@itemId", ItemID);
                cmd.Parameters.AddWithValue("@fkValue", FkValue);
                cmd.Parameters.AddWithValue("@itemName", ItemName);
                cmd.Parameters.AddWithValue("@desc", Description);
                cmd.Parameters.AddWithValue("@price", Price);
                cmd.Parameters.AddWithValue("@stocks", Stocks);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM