private void TeachCourseManageTabPage_Enter(object sender, EventArgs e) { dataTable本学期可教课程.Clear(); dataTable本学期已教课程.Clear(); dataTable以前教授课程.Clear(); dataTableBackground.Clear(); new Thread(new ThreadStart(InitializeSchoolTimeTable)).Start(); //读取本学期已教课程信息 try { //不好意思,我背叛了无产阶级.jpg,只有这个之前教的课完全不需要主键信息,因此只把这个表通过JSON传播 //裸传JSON的问题在于当select语句结果为空集时,传回的DataTable没有列头 //因此只对如下情况选择JSON: //(1)一定不会返回空集 //(2)返回空集时视为报错(比如找不到目标,用例直接终止或重新开始) #region 之前教的课 this.GetServerMessage("teach@previous," + this.textBoxLoginName.Text);//之前教的课 if (ReceiveMessage.IsJson()) { dataTable以前教授课程 = ReceiveMessage.ToDataTable(); dataGridView以前教授课程.DataSource = dataTable以前教授课程; dataGridView以前教授课程.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; foreach (DataGridViewColumn col in dataGridView以前教授课程.Columns) { col.ReadOnly = true; col.SortMode = DataGridViewColumnSortMode.NotSortable; } } else//选课结束 { throw new Exception("选课系统已关闭"); } #endregion 之前教的课 using (MySqlConnection conn = new MySqlConnection(mysqlConnectionString)) { #region 已教课程 conn.Open();//已教课程,直接选择section中id符合条件的即可 MySqlDataAdapter sda = new MySqlDataAdapter(String.Format("SELECT * FROM section where id = {0} and year = {1} and semester = '{2}';", textBoxLoginName.Text, CurrentYear, CurrentSemester), conn); sda.Fill(dataTable本学期已教课程); dataGridView本学期已教课程.DataSource = dataTable本学期已教课程; dataGridView本学期已教课程.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; foreach (DataGridViewColumn col in dataGridView本学期已教课程.Columns) { col.ReadOnly = true; col.SortMode = DataGridViewColumnSortMode.NotSortable; } #endregion 已教课程 #region 填充TimeTable foreach (DataRow sectionRow in dataTable本学期已教课程.Rows) { DataRow[] rows = dataTableTimeSlot.Select( String.Format("time_slot_id = '{0}'", sectionRow[6]));//取出已教课程的time_slot_id foreach (DataRow TimeSlotRow in rows) { int day = (int)Enum.Parse(typeof(星期枚举), (string)TimeSlotRow.ItemArray[1]); //得到课程表第二维坐标 int start_wk = Convert.ToInt32(TimeSlotRow.ItemArray[2]) - 1; //得到课程表第一维循环开始值 int end_wk = Convert.ToInt32(TimeSlotRow.ItemArray[3]) - 1; //得到课程表第一维循环结束值 int start_tm = Convert.ToInt32(TimeSlotRow.ItemArray[4]) - 1; //得到课程表第三维循环开始值 int end_tm = Convert.ToInt32(TimeSlotRow.ItemArray[5]) - 1; //得到课程表第三维循环结束值 for (int i = start_wk; i <= end_wk; i++) { for (int j = start_tm; j <= end_tm; j++) { TimeTable[i, day, j].SetCourseOccupied(); TimeTable[i, day, j].course_id = sectionRow[0].ToString(); TimeTable[i, day, j].sec_id = sectionRow[1].ToString(); TimeTable[i, day, j].semester = sectionRow[2].ToString(); TimeTable[i, day, j].year = sectionRow[3].ToString(); TimeTable[i, day, j].id = sectionRow[7].ToString(); } } } } #endregion 填充TimeTable #region 可教课程 sda = new MySqlDataAdapter(String.Format("select * from section where (course_id in (select course_id from can_teach where id = {0}) and isnull(id)) and year = {1} and semester = '{2}';", textBoxLoginName.Text, CurrentYear, CurrentSemester), conn); sda.Fill(dataTable本学期可教课程); dataGridView本学期可教课程.DataSource = dataTable本学期可教课程; dataGridView本学期可教课程.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; foreach (DataGridViewColumn col in dataGridView本学期可教课程.Columns) { col.ReadOnly = true; col.SortMode = DataGridViewColumnSortMode.NotSortable; } #endregion 可教课程 #region 后台课程 //为已教和可教课程的并集,用于提交数据库时使用 sda = new MySqlDataAdapter(String.Format("select * from section where (course_id in " + "(select course_id from can_teach where id = {0}) " + "and isnull(id)) and year = {1} and semester = '{2}' " + "union SELECT * FROM section where id = {0}" + " and year = {1} and semester = '{2}';", textBoxLoginName.Text, CurrentYear, CurrentSemester), conn); sda.Fill(dataTableBackground); dataTableBackground.PrimaryKey = new DataColumn[] { dataTableBackground.Columns["course_id"], dataTableBackground.Columns["sec_id"], dataTableBackground.Columns["semester"], dataTableBackground.Columns["year"] }; #endregion 后台课程 if (dataTableBackground.Rows.Count == 0) { throw new Exception("没有可选课程"); } sda.Dispose(); conn.Close(); } } catch (MySqlException ex) { MessageBox.Show("无法进入课程目录系统", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }