gpt4 book ai didi

c# - 为什么我的 INSERT 语句会产生重复?

转载 作者:行者123 更新时间:2023-11-30 01:18:18 26 4
gpt4 key购买 nike

我的 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/数据网格正在将集合添加到旧的顶部

最佳答案

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

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

关于c# - 为什么我的 INSERT 语句会产生重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18827030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com