c#添加功能失败,insert语句与foreign key约束冲突,怎么处理

c#添加功能失败,insert语句与foreign key约束冲突,怎么办
错误视图:
c#添加功能失败,insert语句与foreign key约束冲突,怎么处理

相应代码:(SujectId是外键)
  private void button1_Click(object sender, EventArgs e)
        {

            // 查询题目信息的sql语句
            string sql = "insert into Subject (SubjectId)values('" + text5.Text + "');";
            sql = "insert into Question ( QuestionId, Question,Answer,Difficulty,SubjectId,OptionA,OptionB,OptionC,OptionD) values ('" + text1.Text;
            sql += "', '" + text2.Text + "', '" + text3.Text + "', '" + text4.Text + "', '" + text5.Text + "', '" + text6.Text + "', '" + text7.Text + "', '" + text8.Text + "', '" + text9.Text + "');";
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                command.ExecuteNonQuery();

                MessageBox.Show("添加成功!", "提示");

                DataGridViewRowCollection dgrc = dgv.Rows;
                Object[] objs = new Object[4];
                objs[0] = text1.Text;
                objs[1] = text2.Text;
                objs[2] = text3.Text;
                objs[3] = text4.Text;
                objs[4] = text5.Text;
                objs[5] = text6.Text;
                objs[6] = text7.Text;
                objs[7] = text8.Text;
                objs[8] = text9.Text;
                int row = dgrc.Add(objs);
                dgv.CurrentCell = dgv.Rows[row].Cells[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.ToString(), "提示");
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

------解决思路----------------------
引用:
Quote: 引用:

这不是C#的问题,是sql server的报错。

你先搞清楚外键约束
外键约束,比如表B存在一个字段b,有外键约束,引用于表A的主键a,那么在向表B插入数据时,字段b必须为表A中a已经存在的值;如果试图在字段b中存放一个a中没有的值,则会报违反外键约束。

你搞清楚主键约束,你的问题就明朗了。
那主键约束怎么删除啊?

http://msdn.microsoft.com/zh-cn/library/ms189579(v=sql.105).aspx