简体   繁体   中英

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

I have two table the first table name is tbl_Account have a fieldName UserID, Username, Password and the second table name is tbl_Item have a fieldname ItemID, ItemName, UserID. How can I get the value of UserID in tbl_Account and put it to tbl_Item row UserID? Defends to the user who login.

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

You can reference UserID from tbl_Account table in tbl_Item table by writing the following code on your SQL side.

 FOREIGN KEY (UserID) REFERENCES tbl_Account(UserID).

Hope that answers your question.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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