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

时间:2018-09-05 07:41:52

标签: c# oop

我想插入并更新我的表。我的查询语句仅指示主键,项目名称,描述,价格等。问题是我还有另一列是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();
        }

1 个答案:

答案 0 :(得分:0)

您的SQL代码错误。插入记录时必须传递有效的外键值。有效外键值是父表中已经存在的值或NULL。但是,将NULL设置为“外键”列是基于在子表上创建外键的方式。

尝试下面的代码,用子表和值中的正确外键列顺序替换@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();

希望这会有所帮助!