CourseElect.cs
资源名称:VCSDB.rar [点击查看]
上传用户:hjieqiu
上传日期:2013-05-11
资源大小:16494k
文件大小:20k
源码类别:
企业管理
开发平台:
C#
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace 教务管理系统
- {
- /// <summary>
- /// CourseElect 的摘要说明。
- /// </summary>
- public class CourseElect : System.Windows.Forms.Form
- {
- private DataTable electTable=new DataTable();//保存学生的选课信息
- private DataTable courseTable=new DataTable();//保存可选课程信息
- private DataView dv;//可选课程的视图
- private string studentID;//保存选课的学生信息
- private string stduentName;//选课的学生的姓名
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.DataGrid dataGrid1;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.DataGrid dataGrid2;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.TextBox txt1;
- private System.Windows.Forms.TextBox txt2;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.TextBox txt3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.Label label7;
- private System.Windows.Forms.TextBox txt4;
- private System.Windows.Forms.TextBox txt5;
- private System.Windows.Forms.TextBox txt6;
- private System.Windows.Forms.TextBox txt7;
- private System.Windows.Forms.Button btnSearch;
- private System.Windows.Forms.Button btnElect;
- private System.Windows.Forms.Button btnDelete;
- private System.Windows.Forms.Button btnView;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public CourseElect()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CourseElect));
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.txt1 = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.txt2 = new System.Windows.Forms.TextBox();
- this.label2 = new System.Windows.Forms.Label();
- this.txt3 = new System.Windows.Forms.TextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.dataGrid1 = new System.Windows.Forms.DataGrid();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.dataGrid2 = new System.Windows.Forms.DataGrid();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.txt4 = new System.Windows.Forms.TextBox();
- this.label3 = new System.Windows.Forms.Label();
- this.btnSearch = new System.Windows.Forms.Button();
- this.btnElect = new System.Windows.Forms.Button();
- this.btnDelete = new System.Windows.Forms.Button();
- this.btnView = new System.Windows.Forms.Button();
- this.label5 = new System.Windows.Forms.Label();
- this.txt5 = new System.Windows.Forms.TextBox();
- this.label6 = new System.Windows.Forms.Label();
- this.txt6 = new System.Windows.Forms.TextBox();
- this.txt7 = new System.Windows.Forms.TextBox();
- this.label7 = new System.Windows.Forms.Label();
- this.groupBox1.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.txt1);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.txt2);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.txt3);
- this.groupBox1.Controls.Add(this.label4);
- this.groupBox1.Location = new System.Drawing.Point(0, 3);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(696, 53);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "学生信息";
- //
- // txt1
- //
- this.txt1.Location = new System.Drawing.Point(88, 24);
- this.txt1.Name = "txt1";
- this.txt1.TabIndex = 1;
- this.txt1.Text = "";
- this.txt1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt1_KeyPress);
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(40, 28);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(48, 16);
- this.label1.TabIndex = 0;
- this.label1.Text = "学号";
- this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // txt2
- //
- this.txt2.Location = new System.Drawing.Point(272, 24);
- this.txt2.Name = "txt2";
- this.txt2.ReadOnly = true;
- this.txt2.TabIndex = 1;
- this.txt2.Text = "";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(224, 28);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(48, 16);
- this.label2.TabIndex = 0;
- this.label2.Text = "姓名";
- this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // txt3
- //
- this.txt3.Location = new System.Drawing.Point(480, 24);
- this.txt3.Name = "txt3";
- this.txt3.ReadOnly = true;
- this.txt3.TabIndex = 1;
- this.txt3.Text = "";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(432, 28);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(48, 16);
- this.label4.TabIndex = 0;
- this.label4.Text = "班级";
- this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // dataGrid1
- //
- this.dataGrid1.CaptionVisible = false;
- this.dataGrid1.DataMember = "";
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(0, 56);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.ReadOnly = true;
- this.dataGrid1.Size = new System.Drawing.Size(696, 224);
- this.dataGrid1.TabIndex = 1;
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.dataGrid2);
- this.groupBox2.Controls.Add(this.groupBox3);
- this.groupBox2.Location = new System.Drawing.Point(0, 280);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(696, 224);
- this.groupBox2.TabIndex = 2;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "课程信息";
- //
- // dataGrid2
- //
- this.dataGrid2.CaptionVisible = false;
- this.dataGrid2.DataMember = "";
- this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid2.Location = new System.Drawing.Point(216, 16);
- this.dataGrid2.Name = "dataGrid2";
- this.dataGrid2.ReadOnly = true;
- this.dataGrid2.Size = new System.Drawing.Size(472, 200);
- this.dataGrid2.TabIndex = 1;
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.txt4);
- this.groupBox3.Controls.Add(this.label3);
- this.groupBox3.Controls.Add(this.btnSearch);
- this.groupBox3.Controls.Add(this.btnElect);
- this.groupBox3.Controls.Add(this.btnDelete);
- this.groupBox3.Controls.Add(this.btnView);
- this.groupBox3.Controls.Add(this.label5);
- this.groupBox3.Controls.Add(this.txt5);
- this.groupBox3.Controls.Add(this.label6);
- this.groupBox3.Controls.Add(this.txt6);
- this.groupBox3.Controls.Add(this.txt7);
- this.groupBox3.Controls.Add(this.label7);
- this.groupBox3.Location = new System.Drawing.Point(8, 16);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(200, 200);
- this.groupBox3.TabIndex = 0;
- this.groupBox3.TabStop = false;
- //
- // txt4
- //
- this.txt4.Location = new System.Drawing.Point(72, 16);
- this.txt4.Name = "txt4";
- this.txt4.Size = new System.Drawing.Size(112, 21);
- this.txt4.TabIndex = 2;
- this.txt4.Text = "";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(16, 16);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(56, 16);
- this.label3.TabIndex = 1;
- this.label3.Text = "课程拼音";
- //
- // btnSearch
- //
- this.btnSearch.Image = ((System.Drawing.Image)(resources.GetObject("btnSearch.Image")));
- this.btnSearch.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnSearch.Location = new System.Drawing.Point(16, 120);
- this.btnSearch.Name = "btnSearch";
- this.btnSearch.TabIndex = 0;
- this.btnSearch.Text = "查询";
- this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
- //
- // btnElect
- //
- this.btnElect.Enabled = false;
- this.btnElect.Image = ((System.Drawing.Image)(resources.GetObject("btnElect.Image")));
- this.btnElect.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnElect.Location = new System.Drawing.Point(112, 120);
- this.btnElect.Name = "btnElect";
- this.btnElect.TabIndex = 0;
- this.btnElect.Text = "选课";
- this.btnElect.Click += new System.EventHandler(this.btnElect_Click);
- //
- // btnDelete
- //
- this.btnDelete.Enabled = false;
- this.btnDelete.Image = ((System.Drawing.Image)(resources.GetObject("btnDelete.Image")));
- this.btnDelete.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnDelete.Location = new System.Drawing.Point(16, 160);
- this.btnDelete.Name = "btnDelete";
- this.btnDelete.TabIndex = 0;
- this.btnDelete.Text = "删课";
- this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
- //
- // btnView
- //
- this.btnView.Enabled = false;
- this.btnView.Image = ((System.Drawing.Image)(resources.GetObject("btnView.Image")));
- this.btnView.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnView.Location = new System.Drawing.Point(112, 160);
- this.btnView.Name = "btnView";
- this.btnView.TabIndex = 0;
- this.btnView.Text = "看课表";
- this.btnView.Click += new System.EventHandler(this.btnView_Click);
- //
- // label5
- //
- this.label5.Location = new System.Drawing.Point(16, 40);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(56, 16);
- this.label5.TabIndex = 1;
- this.label5.Text = "课程名称";
- //
- // txt5
- //
- this.txt5.Location = new System.Drawing.Point(72, 40);
- this.txt5.Name = "txt5";
- this.txt5.Size = new System.Drawing.Size(112, 21);
- this.txt5.TabIndex = 2;
- this.txt5.Text = "";
- //
- // label6
- //
- this.label6.Location = new System.Drawing.Point(16, 64);
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size(56, 16);
- this.label6.TabIndex = 1;
- this.label6.Text = "课程编号";
- //
- // txt6
- //
- this.txt6.Location = new System.Drawing.Point(72, 64);
- this.txt6.Name = "txt6";
- this.txt6.Size = new System.Drawing.Size(112, 21);
- this.txt6.TabIndex = 2;
- this.txt6.Text = "";
- //
- // txt7
- //
- this.txt7.Location = new System.Drawing.Point(72, 88);
- this.txt7.Name = "txt7";
- this.txt7.Size = new System.Drawing.Size(112, 21);
- this.txt7.TabIndex = 2;
- this.txt7.Text = "";
- //
- // label7
- //
- this.label7.Location = new System.Drawing.Point(16, 88);
- this.label7.Name = "label7";
- this.label7.Size = new System.Drawing.Size(56, 16);
- this.label7.TabIndex = 1;
- this.label7.Text = "课序号";
- //
- // CourseElect
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(696, 509);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.dataGrid1);
- this.Controls.Add(this.groupBox1);
- this.Name = "CourseElect";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "【学生选课】";
- this.Load += new System.EventHandler(this.CourseElect_Load);
- this.groupBox1.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.groupBox2.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- //----------输入学号后回车,显示学生信息和选课信息-----------
- private void txt1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar==13&&txt1.Text.Trim()!="")//输入学号后回车,查询学生信息和选课信息
- {
- this.electTable.Clear();//先清空选课表
- txt2.Clear();
- txt3.Clear();
- //关闭选课,删课等许可
- btnElect.Enabled=false;
- btnDelete.Enabled=false;
- btnView.Enabled=false;
- this.ShowInfo();
- }
- }
- //--------窗体新建时,绑定数据集和读入可选课程信息--------
- private void CourseElect_Load(object sender, System.EventArgs e)
- {
- //绑定dataGrid到学生选课信息上
- dataGrid1.DataSource=this.electTable;
- //读入可选课程数据 string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;"; SqlConnection cn=new SqlConnection(strConn); cn.Open();
- string sql="select a.课序号,a.课程编号,b.课程名称,b.教师,b.开课系别,"
- +"a.上课地点,a.上课时间天,a.上课时间节,b.拼音码"
- +" from 课程表 a,课程信息 b"
- +" where (b.本学期课程='Y')and(a.课程编号=b.课程编号)";
- SqlDataAdapter da2=new SqlDataAdapter(sql,cn);
- da2.Fill(this.courseTable);//可供选择的课程信息
- this.dv=courseTable.DefaultView;
- dataGrid2.DataSource=dv;//绑定显示可选课程信息
- }
- //------显示学生信息和选课信息-----
- private void ShowInfo()//
- {
- string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;"; SqlConnection cn=new SqlConnection(strConn); cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- cmd.CommandText="select a.姓名,b.班级名称,a.学籍编号 from 学生信息 a,班级信息 b "
- +"where(a.班级编号=b.班级编号)and(学号='"+txt1.Text.Trim()+"')";
- SqlDataReader dr=cmd.ExecuteReader();
- dr.Read();//读入数据
- if(!dr.HasRows)//判断学号是否输入正确
- {
- MessageBox.Show("无此学生,请重新输入学号","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
- dr.Close();
- return;
- }
- if(dr.GetValue(2).ToString().Trim()!="1")//判断是否为在籍学生
- {
- MessageBox.Show("该学生并非在籍学生,不能选课","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
- return;
- }
- txt2.Text=dr.GetValue(0).ToString().Trim();//姓名
- txt3.Text=dr.GetValue(1).ToString().Trim();//班级名称
- dr.Close();
- //读入选课信息
- string sql="select a.学号,a.课序号,b.课程名称,b.教师,"
- +"b.开课系别,c.上课时间天,c.上课时间节,c.上课地点"
- +" from 选课表 a,课程信息 b,课程表 c"
- +" where(a.课序号=c.课序号)and('200400000'+b.课程编号=a.课序号)"
- +"and(a.学号='"+txt1.Text.Trim()+"')";
- SqlDataAdapter da=new SqlDataAdapter(sql,cn);
- da.Fill(this.electTable);//该学生的选课信息
- this.studentID=txt1.Text.Trim();//保存获准选课的学生学号
- this.stduentName=txt2.Text.Trim();//保存学生姓名
- //允许选课,删课
- btnElect.Enabled=true;
- btnDelete.Enabled=true;
- btnView.Enabled=true;
- }
- //-----------筛选课程-------------
- private void btnSearch_Click(object sender, System.EventArgs e)
- {
- string filter="";
- if(txt4.Text.Trim()!="")
- {
- filter+="拼音码 like '%"+txt4.Text.Trim()+"%' AND ";
- }
- if(txt5.Text.Trim()!="")
- {
- filter+="课程名称 like '%"+txt5.Text.Trim()+"%' AND ";
- }
- if(txt6.Text.Trim()!="")
- {
- filter+="课程编号='"+txt6.Text.Trim()+"' AND ";
- }
- if(txt7.Text.Trim()!="")
- {
- filter+="课序号 like '%"+txt7.Text.Trim()+"%' AND ";
- }
- if(filter.Length>0)
- {
- filter=filter.Substring(0,filter.Length-5);
- }
- dv.RowFilter=filter;
- }
- //-------------学生选课-----------
- private void btnElect_Click(object sender, System.EventArgs e)
- {
- if(dv.Count==0)//可选课程为空不能选课
- {
- MessageBox.Show("可选课表为空,无法选课","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
- return;
- }
- string courseID=dataGrid2[dataGrid2.CurrentCell.RowNumber,0].ToString().Trim();//当前所选的课序号
- string courseDay=dataGrid2[dataGrid2.CurrentCell.RowNumber,6].ToString().Trim();//所选课程的上课时间天
- string courseSec=dataGrid2[dataGrid2.CurrentCell.RowNumber,7].ToString().Trim();//所选课程的上课时间节
- foreach(DataRow aRow in this.electTable.Rows)
- {
- if(aRow["课序号"].ToString().Trim()==courseID)
- {
- MessageBox.Show("所选课程已经在课程表中","课程重复",MessageBoxButtons.OK,MessageBoxIcon.Stop);
- return;
- }
- if(aRow["上课时间天"].ToString().Trim()==courseDay&&aRow["上课时间节"].ToString().Trim()==courseSec)
- {
- string msg="所选课程和已选课程:"+aRow["课程名称"].ToString().Trim()+" 上课时间冲突";
- MessageBox.Show(msg,"时间冲突",MessageBoxButtons.OK,MessageBoxIcon.Stop);
- return;
- }
- }
- string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;"; SqlConnection cn=new SqlConnection(strConn); cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- cmd.CommandText="insert into 选课表([学号],[课序号]) values('"+this.studentID+"','"+courseID+"')";
- cmd.ExecuteNonQuery();
- //重新读入学生的选课信息
- this.electTable.Clear();
- string sql="select a.学号,a.课序号,b.课程名称,b.教师,"
- +"b.开课系别,c.上课时间天,c.上课时间节,c.上课地点"
- +" from 选课表 a,课程信息 b,课程表 c"
- +" where(a.课序号=c.课序号)and('200400000'+b.课程编号=a.课序号)"
- +"and(a.学号='"+this.studentID+"')";
- SqlDataAdapter da=new SqlDataAdapter(sql,cn);
- da.Fill(this.electTable);//该学生的选课信息
- }
- //---------删除原来所选的课程------------
- private void btnDelete_Click(object sender, System.EventArgs e)
- {
- if(this.electTable.Rows.Count==0)
- {
- return;//无课可删,不作任何动作
- }
- string deleteID=dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString().Trim();//要删除的课序号
- string deleteName=dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString().Trim();//要删除的课名称
- string msg="是否确认删除已选课程:"+deleteName+" ?";
- DialogResult result=MessageBox.Show(msg,"删除课程",MessageBoxButtons.OKCancel);
- if(result==DialogResult.OK)
- {
- string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;"; SqlConnection cn=new SqlConnection(strConn); cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- cmd.CommandText="delete from 选课表 where(学号='"+this.studentID+"')and(课序号='"+deleteID+"')";
- cmd.ExecuteNonQuery();
- //重新读入学生的选课信息
- this.electTable.Clear();
- string sql="select a.学号,a.课序号,b.课程名称,b.教师,"
- +"b.开课系别,c.上课时间天,c.上课时间节,c.上课地点"
- +" from 选课表 a,课程信息 b,课程表 c"
- +" where(a.课序号=c.课序号)and('200400000'+b.课程编号=a.课序号)"
- +"and(a.学号='"+this.studentID+"')";
- SqlDataAdapter da=new SqlDataAdapter(sql,cn);
- da.Fill(this.electTable);//该学生的选课信息
- }
- }
- //---------显示当前选课学生的课表------------
- private void btnView_Click(object sender, System.EventArgs e)
- {
- CurriculumSchedule newFrm=new CurriculumSchedule(this.studentID);
- newFrm.Text+="学生:"+this.stduentName;
- newFrm.ShowDialog();
- }
- }
- }