⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scoreinput.cs

📁 数据库课程设计__选修课管理系统__含有SQL数据库和全部源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 选修课管理系统
{
	/// <summary>
	/// ScoreInput 的摘要说明。
	/// </summary>
	public class ScoreInput : System.Windows.Forms.Form
	{
		private string courseID;//当前所列成绩课程的课程编号
		private DataTable scoreTable=new DataTable();//保存某一门课的成绩单
		private SqlDataAdapter da;//用来填充成绩表的数据适配器
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button btnEndElect;
		private System.Windows.Forms.Button btnQuery;
		private System.Windows.Forms.Button btnSubmit;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txt2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txt3;
		private System.Windows.Forms.Button btnModify;
		private System.Windows.Forms.TextBox txt4;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ScoreInput()
		{
			//
			// 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()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnSubmit = new System.Windows.Forms.Button();
			this.btnQuery = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.txt2 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.txt3 = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnEndElect = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txt4 = new System.Windows.Forms.TextBox();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.btnModify = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnSubmit);
			this.groupBox1.Controls.Add(this.btnQuery);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.txt2);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.txt3);
			this.groupBox1.Location = new System.Drawing.Point(0, 64);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(632, 144);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "选择课程";
			// 
			// btnSubmit
			// 
			this.btnSubmit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnSubmit.Location = new System.Drawing.Point(152, 112);
			this.btnSubmit.Name = "btnSubmit";
			this.btnSubmit.Size = new System.Drawing.Size(96, 23);
			this.btnSubmit.TabIndex = 2;
			this.btnSubmit.Text = "最终提交";
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			// 
			// btnQuery
			// 
			this.btnQuery.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnQuery.Location = new System.Drawing.Point(24, 112);
			this.btnQuery.Name = "btnQuery";
			this.btnQuery.Size = new System.Drawing.Size(104, 23);
			this.btnQuery.TabIndex = 1;
			this.btnQuery.Text = "查询";
			this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(280, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(312, 88);
			this.label1.TabIndex = 0;
			this.label1.Text = "选择一门课,列出选该课的所有学生名单,教师对各个学生的成绩输入,并保存,最终提交之前还可以修改。最终提交后的成绩不可修改";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(56, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "课程编号";
			// 
			// txt2
			// 
			this.txt2.Location = new System.Drawing.Point(112, 24);
			this.txt2.Name = "txt2";
			this.txt2.TabIndex = 3;
			this.txt2.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(56, 72);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 4;
			this.label4.Text = "课程名称";
			// 
			// txt3
			// 
			this.txt3.Location = new System.Drawing.Point(112, 64);
			this.txt3.Name = "txt3";
			this.txt3.TabIndex = 3;
			this.txt3.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.btnEndElect);
			this.groupBox2.Location = new System.Drawing.Point(0, 0);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(632, 64);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "结束选课,生成空成绩单";
			// 
			// btnEndElect
			// 
			this.btnEndElect.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnEndElect.Location = new System.Drawing.Point(216, 16);
			this.btnEndElect.Name = "btnEndElect";
			this.btnEndElect.Size = new System.Drawing.Size(120, 40);
			this.btnEndElect.TabIndex = 0;
			this.btnEndElect.Text = "结束选课";
			this.btnEndElect.Click += new System.EventHandler(this.btnEndElect_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.txt4);
			this.groupBox3.Controls.Add(this.dataGrid1);
			this.groupBox3.Controls.Add(this.btnModify);
			this.groupBox3.Location = new System.Drawing.Point(0, 216);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(632, 296);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "课程成绩输入";
			// 
			// txt4
			// 
			this.txt4.Location = new System.Drawing.Point(528, 64);
			this.txt4.Name = "txt4";
			this.txt4.TabIndex = 3;
			this.txt4.Text = "";
			this.txt4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt4_KeyPress);
			// 
			// dataGrid1
			// 
			this.dataGrid1.BackgroundColor = System.Drawing.Color.MistyRose;
			this.dataGrid1.CaptionVisible = false;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 16);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(512, 272);
			this.dataGrid1.TabIndex = 0;
			// 
			// btnModify
			// 
			this.btnModify.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnModify.Location = new System.Drawing.Point(528, 104);
			this.btnModify.Name = "btnModify";
			this.btnModify.Size = new System.Drawing.Size(96, 23);
			this.btnModify.TabIndex = 2;
			this.btnModify.Text = "修改成绩";
			this.btnModify.Click += new System.EventHandler(this.btnModify_Click);
			// 
			// ScoreInput
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(632, 517);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "ScoreInput";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "【成绩录入管理】";
			this.Load += new System.EventHandler(this.ScoreInput_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		//-------------绑定dataGrid显示成绩表数据----------
		private void ScoreInput_Load(object sender, System.EventArgs e)
		{
			//绑定数据表显示
			dataGrid1.DataSource=this.scoreTable;		
			
		}
		
		//---------调用储存过程,结束选课并生成新的空成绩单---------
		private void btnEndElect_Click(object sender, System.EventArgs e)
		{
			string conStr="workstation id=localhost;Integrated Security=SSPI;Database=tou";
			SqlConnection cn=new SqlConnection(conStr);
			cn.Open();
			SqlCommand cmd=cn.CreateCommand();
			cmd.CommandText="exec sf_终止选课";
			try
			{
				cmd.ExecuteNonQuery();
				MessageBox.Show("终止选课成功,将生成新的空成绩单","操作成功",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch(Exception express)
			{
				MessageBox.Show(express.ToString(),"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}		
		}

		//------------根据输入查询某门课程,并将选该课的学生成绩列于表中---------
		private void btnQuery_Click(object sender, System.EventArgs e)
		{
			string sql="select a.编号,a.课程编号,b.课程名称,a.学号,c.姓名,a.成绩 from 成绩表 a,课程信息 b,学生信息 c "
				+"where(a.课程编号=b.课程编号)and(a.学号=c.学号)and(a.是否已确定成绩='N')"
				+"and(a.课程编号 like '%"+txt2.Text.Trim()+"%')and(b.课程名称 like '%"+txt3.Text.Trim()+"%')";
			string conStr="workstation id=localhost;Integrated Security=SSPI;Database=tou";
			SqlConnection cn=new SqlConnection(conStr);
			cn.Open();
			da=new SqlDataAdapter(sql,cn);
			this.scoreTable.Clear();//清空表格,重新填充数据
			da.Fill(this.scoreTable);
			if(scoreTable.Rows.Count>0)//表格不为空时,开始允许设置学生成绩
			{
				//保存该课程的课程编号,用来为最终提交成绩的储存过程提供参数
				this.courseID=this.scoreTable.Rows[0][1].ToString().Trim();
			}
			else
			{
				string msg="该表中没有数据,可能原因有:\n"+"1.本学期没有该门课程\n"+"2.该课程无学生选课\n"
					+"3.该课程所有学生成绩已经被最终提交,不可再修改";
				MessageBox.Show(msg,"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			txt4.DataBindings.Clear();
			//绑定成绩显示
			txt4.DataBindings.Add("Text",this.scoreTable,"成绩");			
		}		

		//--------------修改表中学生的成绩--------------
		private void btnModify_Click(object sender, System.EventArgs e)
		{
			if(this.scoreTable.Rows.Count==0)//检查成绩单中是否有学生名单
			{
				MessageBox.Show("该门课程成绩单为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
				return;
			}	
			if(txt4.Text.Trim()=="")//检查成绩字段
			{
				MessageBox.Show("必须输入学生成绩","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
				return;
			}	
		
			//更新学生成绩
			string conStr="workstation id=localhost;Integrated Security=SSPI;Database=tou";
			SqlConnection cn=new SqlConnection(conStr);
			cn.Open();
			SqlCommand cmd=cn.CreateCommand();
			string studentNum=dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString().Trim();
			cmd.CommandText="update 成绩表 set 成绩='"+txt4.Text.Trim()+"' where 编号='"+studentNum+"'";
			cmd.ExecuteNonQuery();
			//更新表中的显示			
			this.scoreTable.Clear();
			da.Fill(this.scoreTable);
            		
		}

		//------------在【成绩】文本框中只允许输入数字和小数点------------
		private void txt4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(!((e.KeyChar<='9'&&e.KeyChar>='0')||e.KeyChar=='.'||e.KeyChar==8))//如果按下键不是数字或者小数点或者删除键,则不响应
				e.Handled=true;
		}

		//------------最终提交成绩------------
		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
			string conStr="workstation id=localhost;Integrated Security=SSPI;Database=tou";
			SqlConnection cn=new SqlConnection(conStr);
			cn.Open();
			SqlCommand cmd=cn.CreateCommand();
			cmd.CommandText="exec sf_成绩提交 "+this.courseID;
			DialogResult result;
			result=MessageBox.Show("最终提交后学生成绩即不可修改,是否确定提交","提交成绩",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
			if(result==DialogResult.OK)
			{
				try
				{
					cmd.ExecuteNonQuery();
					MessageBox.Show("成绩最终提交成功","操作成功",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
				catch(Exception express)
				{
					MessageBox.Show(express.ToString(),"操作失败",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}	
				//更新表中的显示			
				this.scoreTable.Clear();
				da.Fill(this.scoreTable);
			}

		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -