我正在尝试使用以下代码将数据插入表alunos
:
string query = "INSERT INTO alunos (codaluno, nivel, Nome, turma, Estado, Morada, codpostal,Telefone, NIF, datanasc, Ano, Data_entrada, Data_saida, codescola, codpagamento, codpais) " +
"VALUES (@codaluno, @nivel, @Nome, @turma, @Estado, @Morada, @codpostal, @Telefone, @NIF, @datanasc, @Ano, @Data_entrada, @Data_saida, @codescola, @codpagamento, @codpais);";
if(a.open_connection())
{
MySqlCommand cmd = new MySqlCommand(query, a.connection);
cmd.Parameters.AddWithValue("@codaluno", textBox1.Text);
cmd.Parameters.AddWithValue("@Nome", textBox2.Text);
cmd.Parameters.AddWithValue("@turma", textBox3.Text);
cmd.Parameters.AddWithValue("@Estado", textBox4.Text);
cmd.Parameters.AddWithValue("@Morada", textBox5.Text);
cmd.Parameters.AddWithValue("@codpostal", textBox6.Text);
cmd.Parameters.AddWithValue("@telefone", textBox7.Text);
cmd.Parameters.AddWithValue("@NIF", textBox8.Text);
cmd.Parameters.AddWithValue("@datanasc", textBox9.Text);
cmd.Parameters.AddWithValue("@Ano", textBox10.Text);
cmd.Parameters.AddWithValue("@Data_entrada", textBox11.Text);
cmd.Parameters.AddWithValue("@Data_saida", textBox12.Text);
cmd.Parameters.AddWithValue("@codescola", textBox13.Text);
cmd.Parameters.AddWithValue("@codpagamento", textBox14.Text);
cmd.Parameters.AddWithValue("@nivel", textBox15.Text);
cmd.Parameters.AddWithValue("@codpais", textBox16.Text);
cmd.ExecuteNonQuery();
a.close_connection();
}
但是由于外键(在这种情况下是“ codescola”,“ codpagamento”,“ nivel”和“ codpais”)而导致错误。
这是错误:
无法添加或更新子行:外键约束失败(
edacademy
。alunos
,CONSTRAINTalunos_ibfk_3
外键(codescola
)参考escolas
(codescola
)
你们能帮我吗?