如何为外键保存价值?

时间:2015-09-24 15:49:58

标签: c# mysql .net

我希望在subjectname中保存所选DataGridView的ID,以及在组合框中选择年级的学生的ID。因此,我获得学生和学科的参考是学年和年级。我想将其保存在包含Registration_registrationID(FK),Student_studentID(FK),yearlevelschoolyear的表格中:

这是我的代码:

private void btn_add_Click(object sender, EventArgs e)
    {
        string reg = "";
        string schoolyear = txt_startYr.Text +  "-" + txt_endYr.Text;
        MySqlConnection conn, conn1, conn2;
        MySqlCommand cmd2;
        MySqlDataReader reader, reader1;

        List<DataGridViewRow> selectedRows = (from row in dg_subjects.Rows.Cast<DataGridViewRow>() where Convert.ToBoolean(row.Cells[3].Value) == true select row).ToList();

        if (selectedRows.Count >= 1)
        {
            if (MessageBox.Show(string.Format("Are you sure you want to add this subject?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in selectedRows)
                {
                    try
                    {                      
                       conn = new MySqlConnection(myconn);
                       conn.Open();
                       string subject = "Select subjectID from southpoint_school.subject WHERE subject_name = '" + row.Cells[0].Value.ToString() + "';";
                       MySqlCommand cmd = new MySqlCommand(subject, conn);
                       reader = cmd.ExecuteReader();
                       reader.Read();
                       string subj = reader.GetString("subjectID");
                       conn.Close();

                       conn1 = new MySqlConnection(myconn);
                       conn1.Open();
                       string query = "Select regID from southpoint_school.registration where yearLevel =  '" + cmb_year.Text + "' AND  schoolYear = '" + schoolyear + "';";
                       MySqlCommand cmd1 = new MySqlCommand(query, conn1);
                       reader1 = cmd1.ExecuteReader();

                       while (reader1.Read())
                       {

                           conn2 = new MySqlConnection(myconn);
                           reg = reader1.GetString("regID");
                           string query1 = "INSERT INTO southpoint_school.schedule (Registration_regID, Subject_subjectID, yearLevel, school_year) values ('" + reg + "','" + subj + "','" + cmb_year.Text + "','"  + schoolyear + "')";
                           cmd2 = new MySqlCommand(query1, conn2);
                           conn2.Open();
                           cmd2.ExecuteNonQuery();
                           MessageBox.Show("Successfully Saved");
                           conn2.Close();
                       }
                       conn1.Close();
                  }



                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                  }
            }
        }

        else
        {

            MessageBox.Show("Please select a Subject");
        }

      }

它不会出错,但它什么都不做。

0 个答案:

没有答案