在我的应用程序中已经与主键约束和外键冲突。我在编程语言c#和SqlDataReader中使用存储过程。如何将数据添加到数据库中的外键,没有主键约束。作为一种组织方式的例子?
我的firstTable代码:
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@io", SqlDbType.Int);
cmd.Parameters["@io"].Direction = ParameterDirection.Input;
cmd.Parameters["@io"].Value = 1;
cmd.Parameters.Add("@idstud", SqlDbType.BigInt);
cmd.Parameters["@idstud"].Direction = ParameterDirection.Input;
cmd.Parameters["@idstud"].Value = id_studtextBox.Text;
cmd.Parameters.Add("@numb_stud", SqlDbType.Int);
cmd.Parameters["@numb_stud"].Direction = ParameterDirection.Input;
cmd.Parameters["@numb_stud"].Value = numberstudtextbox.Text;
cmd.Parameters.Add("@naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["@naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["@naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("@place", SqlDbType.Int);
cmd.Parameters["@place"].Direction = ParameterDirection.Input;
cmd.Parameters["@place"].Value = placetextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_id_stud].ToString(),
(string)rdr[_nomer_stud].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_mesto].ToString());
}
secondTable的代码
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@daw", SqlDbType.Int);
cmd.Parameters["@daw"].Direction = ParameterDirection.Input;
cmd.Parameters["@daw"].Value = 3;
cmd.Parameters.Add("@teachid", SqlDbType.BigInt);
cmd.Parameters["@teachid"].Direction = ParameterDirection.Input;
cmd.Parameters["@teachid"].Value = id_cabtextBox.Text;
cmd.Parameters.Add("@numb_teacher", SqlDbType.Int);
cmd.Parameters["@numb_teacher"].Direction = ParameterDirection.Input;
cmd.Parameters["@numb_teacher"].Value = numbercabtextbox.Text;
cmd.Parameters.Add("@naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["@naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["@naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("@studid", SqlDbType.Int);
cmd.Parameters["@studid"].Direction = ParameterDirection.Input;
cmd.Parameters["@studid"].Value = studidtextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_teacher_id].ToString(),
(string)rdr[_nomer_teacher].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_studid].ToString());
}