我的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中调用了我的人员表,并且INSERT没有被重复,我的SELECT /数据网格正在旧的上面添加集合
答案 0 :(得分: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);
}
}