0
I’m having trouble inserting data into an N:N table. The two tables in question are: Course and Subject. The relationship between them forms a table called tbMATERIA_CURSO
which stores the primary keys of the two tables.
Code to insert in table tbCURSO
:
Conexao conexao = new Conexao();
SqlCommand sql = new SqlCommand();
public void insereCurso(string nomeCurso, string periodo, string tipo)
{
sql.CommandText = "insert into tbCURSO(NomeCurso, Periodo, TipoEnsino) VALUES (@nome_curso, @periodo, @tipoEnsino)";
// parâmetros
sql.Parameters.AddWithValue("@nome_curso", nomeCurso);
sql.Parameters.AddWithValue("@periodo", periodo);
sql.Parameters.AddWithValue("@tipoEnsino", tipo);
try
{
sql.Connection = conexao.Conectar();
sql.ExecuteNonQuery();
conexao.Desconectar();
MessageBox.Show("Curso cadastrado com sucesso", "Sucesso", MessageBoxButtons.OK , MessageBoxIcon.Information);
} catch (SqlException e)
{
MessageBox.Show(e.Message, "Erro ao tentar se conectar com o Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Code to insert in table tbMATERIA
:
Conexao conexao = new Conexao();
SqlCommand sql = new SqlCommand();
public void InsereMaterias(string nomeMateria, int cargaHoraria)
{
sql.CommandText = "insert into tbMATERIA(NomeMateria, CargaHoraria) VALUES (@nome_materia, @carga_horaria)";
// parâmetros
sql.Parameters.AddWithValue("@nome_materia", nomeMateria);
sql.Parameters.AddWithValue("@carga_horaria", cargaHoraria);
try
{
sql.Connection = conexao.Conectar();
sql.ExecuteNonQuery();
conexao.Desconectar();
MessageBox.Show("Curso cadastrado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException e)
{
MessageBox.Show(e.Message, "Erro ao tentar se conectar com o Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
How can I enter the primary key created on tbCURSO
and in the tbMATERIA
as a foreign key in tbMATERIA_CURSO
?
https://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row
– anonimo
I don’t know what this helps me with.
– Bruno Oliveira
As you are using artificial keys understand that your difficulty is to get the values of Idcurso and Idmateria that were assigned at the time of insertion of the records in the respective tables to be able to insert in tbMATERIA_CURSO.
– anonimo
Can each subject belong to one or more courses? Shouldn’t it belong to just one? There may exist several subjects with the same name (even so I find strange, but I believe there may be), but one belonging to two or more courses?
– João Martins
Yes, for example, the Portuguese language discipline can be contained in all registered courses, Computer Technician, Administration, Accounting and etc.
– Bruno Oliveira
@Brunooliveira A tip: don’t use the tag
visual-studio
for questions that are not related to the tool itself (text editing, code analysis, plugins, among others). If you have any questions about this, it might be a good idea to read the post What is a programming language, IDE and compiler?– Jéf Bueno