📄 selectcourse.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace StudentManager
{
public partial class SelectCourse : Form
{
private SqlConnection conn1 = null;
private string selectstr = null;
private SqlCommand Command11 = null;
private SqlDataReader DataReader = null;
private DataSet Dataset=new DataSet("SelectCourse");
private SqlDataAdapter DataAdapter = null;
private DataRow newrow = null;
public SelectCourse()
{
InitializeComponent();
conn1 = new SqlConnection(DataLevel.Connection.ConnString);
selectstr = "Select Department from SelectCourse";
Command11 = new SqlCommand();
Command11.CommandText = selectstr;
Command11.Connection = conn1;
}
private void SelectCourse_Load(object sender, EventArgs e)
{
if (conn1.State == ConnectionState.Closed)
this.conn1.Open();
this.selectstr = "select DepartmentName from Department";
this.Command11.CommandText = selectstr;
this.DataReader = this.Command11.ExecuteReader();
while (this.DataReader.Read())
{
this.comboBoxDepartment.Items.Add(this.DataReader["DepartmentName"].ToString());
}
this.DataReader.Close();
selectstr = "Select SemesterName from Semester";
this.Command11.CommandText = selectstr;
this.DataReader = this.Command11.ExecuteReader();
while (this.DataReader.Read())
{
this.comboBoxSemester.Items.Add(this.DataReader["SemesterName"].ToString());
}
this.DataReader.Close();
this.selectstr = "Select CourseName from Course";
this.Command11.CommandText = selectstr;
this.DataReader = this.Command11.ExecuteReader();
while (DataReader.Read())
{
listBoxAll.Items.Add(this.DataReader["CourseName"].ToString());
}
this.DataReader.Close();
this.conn1.Close();
this.btnAdd.Enabled = false;
this.btnDel.Enabled = false;
this.btnOk.Enabled = false;
}
private void comboBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
this.conn1.Open();
this.comboBoxClass.Items.Clear();
this.listBoxHave.Items.Clear();
this.comboBoxClass.Text = "";
this.selectstr = "select ClassName from Class where DepartmentName=" + "'" + this.comboBoxDepartment.Text + "'";
this.Command11.CommandText = this.selectstr;
this.DataReader = this.Command11.ExecuteReader();
while (this.DataReader.Read())
{
this.comboBoxClass.Items.Add(this.DataReader["ClassName"].ToString());
}
this.DataReader.Close();
this.conn1.Close();
}
private void btnApply_Click(object sender, EventArgs e)
{
if (this.comboBoxClass.Text == "" | comboBoxDepartment.Text == "" | comboBoxSemester.Text == "")
{
MessageBox.Show("学院 班级 和 学期代码不能为空 !否则不能成功选课 !");
return;
}
Listbox_fill();
this.btnAdd.Enabled = true;
this.btnDel.Enabled = true;
this.btnOk.Enabled = true;
}
private void Listbox_fill()
{
if(conn1.State==ConnectionState.Closed)
this.conn1.Open();
selectstr = "Select Course from SelectCourse where Department = '" + this.comboBoxDepartment.Text + "'and Class= '" +
this.comboBoxClass.Text + "'and Semester = '" + this.comboBoxSemester.Text + "'";
this.Command11.CommandText = selectstr;
this.DataReader = this.Command11.ExecuteReader();
while (this.DataReader.Read())
{
listBoxHave.Items.Add(this.DataReader["Course"].ToString());
}
this.DataReader.Close();
MessageBox.Show(this.comboBoxDepartment.Text+"学院 "+this.comboBoxClass.Text+"班级 "+"现在开始 \""+this.comboBoxSemester.Text+"\" 选课 !");
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (this.listBoxAll.Items.Count == 0)
{
MessageBox.Show("目前还没有预选的课 !");
return;
}
else
{
for (int i = 0; i < listBoxHave.Items.Count; i++)
{
if (listBoxAll.SelectedItem.Equals(this.listBoxHave.Items[i]))
{
MessageBox.Show("此课程已选 ,请从新选过!");
return;
}
}
this.DataReader.Close();
// this.selectstr = "select ClassName from Class where DepartmentName=" + "'" + this.comboBoxDepartment.Text + "'";
selectstr = "Select * from SelectCourse where Department = '" + this.comboBoxDepartment.Text + "'and Class= '" +
this.comboBoxClass.Text + "'and Semester = '" + this.comboBoxSemester.Text + "'";
this.DataAdapter = new SqlDataAdapter(selectstr, this.conn1);
// this.Dataset.Clear();
this.DataAdapter.Fill(this.Dataset, "SelectCourse");
SqlCommandBuilder commadbuider = new SqlCommandBuilder(this.DataAdapter);
newrow = Dataset.Tables[0].NewRow();
newrow["Department"] = this.comboBoxDepartment.Text;
newrow["Class"] = this.comboBoxClass.Text;
newrow["Semester"] = this.comboBoxSemester.Text;
newrow["Course"] = this.listBoxAll.SelectedItem.ToString();
this.Dataset.Tables[0].Rows.Add(newrow);
this.listBoxHave.Items.Add(this.listBoxAll.SelectedItem);
MessageBox.Show("课程添加成功!");
this.conn1.Close();
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (conn1.State == ConnectionState.Closed)
{
this.conn1.Open();
}
SqlCommandBuilder commandbuiter = new SqlCommandBuilder(this.DataAdapter);
this.DataAdapter.Update(this.Dataset, "SelectCourse");
this.Dataset.AcceptChanges();
this.conn1.Close();
}
private void btnDel_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Dataset.Tables[0].Rows.Count; i++)
{
if (this.Dataset.Tables[0].Rows[i]["Course"].ToString() == this.listBoxHave.SelectedItem.ToString())
{
this.Dataset.Tables[0].Rows[i].Delete();
this.listBoxHave.Items.RemoveAt(this.listBoxHave.SelectedIndex);
MessageBox.Show("删除成功!");
return;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -