簡體   English   中英

如果關系是一對多,如何在我的外鍵列上插入值

[英]How can I insert value on my Foreign Key Column if the relationship is one-to-many

我有兩個表,第一個表名稱是tbl_Account,具有fieldName用戶ID,用戶名,密碼,第二個表名稱是tbl_Item,具有域名稱ItemID,ItemName,UserID。 如何獲取tbl_Account中UserID的值並將其放入tbl_Item行UserID? 捍衛登錄用戶。

    public void InsertRecord()
            {
                Connection connection = new Connection();
                try
                {
                    string sql = "INSERT INTO tbl_Item VALUES (@itemId, @itemName, @logId)";
                    MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@itemId", GenerateID());
                    cmd.Parameters.AddWithValue("@itemName", ItemName);
                    cmd.Parameters.AddWithValue("@logId", GenerateIDs());
                    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);
                }
            }

//my code for btnAdd
private void button1_Click(object sender, EventArgs e)
        {
            Item item = new Item();

            item.ItemID = item.GenerateID();
            item.ItemName = textBox2.Text;
            item.Account.UserID = item.Account.GenerateID();
            item.InsertRecord();
        }

通過在SQL端編寫以下代碼,可以從tbl_Item表的tbl_Account表中引用UserID。

 FOREIGN KEY (UserID) REFERENCES tbl_Account(UserID).

希望這能回答你的問題。

public void InsertRecord(Account loggedInUser)
            {
                Connection connection = new Connection();
                try
                {
                    string sql = "INSERT INTO tbl_Item VALUES (@itemId, @itemName, @logId)";
                    MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@itemId", ItemId)); // Why you called GenerateID() second time? It was generated in button1_Click
                    cmd.Parameters.AddWithValue("@itemName", ItemName);
                    cmd.Parameters.AddWithValue("@logId", loggedInUser.UserID);
                    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);
                }
            }

//my code for btnAdd
private void button1_Click(object sender, EventArgs e)
        {
            Item item = new Item();

            item.ItemID = item.GenerateID();
            item.ItemName = textBox2.Text;
            //item.Account.UserID = item.Account.GenerateID(); do you really need this?
// someLoggedInUser is Account
            item.InsertRecord(someLoggedInUser);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM