0

我的 INSERT 语句给我每个 INSERT 两次,如下所示:

http://i.imgur.com/cMRiXfk.png

public static void insertStudent(int personId, string firstName, string lastName, string DOB, int phoneNumber, string address, int postCode, string majorField, int gradePointAverage, int Person_personId)
{
    try
    {
        MySqlConnection conn = connection();
        conn.Open();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = conn;
        string myInsertSQL = "INSERT INTO person(personId, firstName, lastName, DOB, phoneNumber, address, postCode) VALUES (@personId, @firstName, @lastName, @DOB, @phoneNumber, @address, @postCode)";
        cmd.Prepare();
        cmd.CommandText = myInsertSQL;
        cmd.Parameters.AddWithValue("@personId", personId);
        cmd.Parameters.AddWithValue("@firstName", firstName);
        cmd.Parameters.AddWithValue("@lastName", lastName);
        cmd.Parameters.AddWithValue("@DOB", DOB);
        cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
        cmd.Parameters.AddWithValue("@address", address);
        cmd.Parameters.AddWithValue("@postCode", postCode);
        cmd.ExecuteNonQuery();

        string myInsertSQLStudent = "INSERT INTO student(majorField, gradePointAverage, person_personId) VALUES (@majorField, @gradePointAverage, @person_personId)";
        cmd.Prepare();
        cmd.CommandText = myInsertSQLStudent;
        cmd.Parameters.AddWithValue("@person_personId", Person_personId);
        cmd.Parameters.AddWithValue("@majorField", majorField);
        cmd.Parameters.AddWithValue("@gradePointAverage", gradePointAverage);
        prevID(conn, cmd);
    }
    catch
    {
        MessageBox.Show("Invalid User Name or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

我有两个 INSERT 语句,因为有些文本框必须转到一个表,而有些则必须转到另一个表。

编辑:

这是调用它的按钮:

 private void btnInsertStudentNumberAdmin_Click(object sender, EventArgs e)
 {
     studentHelperClass.insertStudent(int.Parse(txtPersonIDStudent.Text), txtFirstNameStudent.Text, txtLastNameStudent.Text, txtDOBStudent.Text, int.Parse(txtPhoneNumberStudent.Text), txtAddressStudent.Text, int.Parse(txtPostCodeStudent.Text), txtMajorFieldStudent.Text, int.Parse(txtGpaStudent.Text), int.Parse(txtPerson_PersonIdStudent.Text));
 }

抱歉编辑:

所以我只是在 mySql 中调用了我的 person 表并且 INSERT 没有被重复,我的 SELECT/data 网格正在将集合添加到旧的集合之上

4

1 回答 1

0

尝试创建一个新命令,不要为第二次插入使用相同的命令:

    public static void insertStudent(int personId, string firstName, string lastName, string DOB, int phoneNumber, string address, int postCode, string majorField, int gradePointAverage, int Person_personId)
    {
        try
        {
            MySqlConnection conn = connection();
            conn.Open();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            string myInsertSQL = "INSERT INTO person(personId, firstName, lastName, DOB, phoneNumber, address, postCode) VALUES (@personId, @firstName, @lastName, @DOB, @phoneNumber, @address, @postCode)";
            cmd.Prepare();
            cmd.CommandText = myInsertSQL;
            cmd.Parameters.AddWithValue("@personId", personId);
            cmd.Parameters.AddWithValue("@firstName", firstName);
            cmd.Parameters.AddWithValue("@lastName", lastName);
            cmd.Parameters.AddWithValue("@DOB", DOB);
            cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@postCode", postCode);
            cmd.ExecuteNonQuery();

            MySqlCommand cmd2 = new MySqlCommand();
            cmd2.Connection = conn;
            string myInsertSQLStudent = "INSERT INTO student(majorField, gradePointAverage, person_personId) VALUES (@majorField, @gradePointAverage, @person_personId)";
            cmd2.Prepare();
            cmd2.CommandText = myInsertSQLStudent;
            cmd2.Parameters.AddWithValue("@person_personId", Person_personId);
            cmd2.Parameters.AddWithValue("@majorField", majorField);
            cmd2.Parameters.AddWithValue("@gradePointAverage", gradePointAverage);
            prevID(conn, cmd2);
        }
        catch
        {
            MessageBox.Show("Invalid User Name or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
于 2013-09-16T11:42:06.927 回答