错误视图:
相应代码:(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();
}
}