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

📄 course.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>
	/// Course 的摘要说明。
	/// </summary>
	public class Course : System.Windows.Forms.Form
	{
		private static string newID;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtClass;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox cobCourse;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnQuit;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Course()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.txtClass = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.cobCourse = new System.Windows.Forms.ComboBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnQuit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 24);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "班级";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtClass
			// 
			this.txtClass.Location = new System.Drawing.Point(152, 26);
			this.txtClass.Name = "txtClass";
			this.txtClass.ReadOnly = true;
			this.txtClass.TabIndex = 1;
			this.txtClass.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(32, 64);
			this.label2.Name = "label2";
			this.label2.TabIndex = 2;
			this.label2.Text = "课程";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// cobCourse
			// 
			this.cobCourse.Items.AddRange(new object[] {
														   "数学",
														   "英语",
														   "计算机",
														   "程序设计",
														   "程序开发"});
			this.cobCourse.Location = new System.Drawing.Point(152, 64);
			this.cobCourse.Name = "cobCourse";
			this.cobCourse.Size = new System.Drawing.Size(121, 20);
			this.cobCourse.TabIndex = 0;
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(47, 128);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 4;
			this.btnSave.Text = "保存";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnQuit
			// 
			this.btnQuit.Location = new System.Drawing.Point(175, 128);
			this.btnQuit.Name = "btnQuit";
			this.btnQuit.TabIndex = 5;
			this.btnQuit.Text = "退出";
			this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
			// 
			// Course
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(296, 182);
			this.Controls.Add(this.btnQuit);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.cobCourse);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtClass);
			this.Controls.Add(this.label1);
			this.Name = "Course";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "添加课程";
			this.Load += new System.EventHandler(this.Course_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Course_Load(object sender, System.EventArgs e)
		{
			this.txtClass.Text=AddCourse.UserID;

		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(this.cobCourse.Text!="")
			{
				try
				{
					string newID=this.GetMaxID();
					string PerID=this.GetMaxPID();
					string courseID=this.GetCourseID();
					string classID=this.GetClassID();

					string StrSQL="workstation id=localhost;Integrated Security=SSPI;database=stu01";
					SqlConnection conn=new SqlConnection(StrSQL);
					conn.Open();
					SqlCommand cmd=conn.CreateCommand();
					cmd.CommandText="insert into 成绩表([序号],[学号],[课程编号],[班级编号])"
						+" values('"+newID.Trim()+"','"+PerID.Trim()+"','"+courseID+"','"+classID+"')";
					cmd.ExecuteNonQuery();
					MessageBox.Show(label1.Text+txtClass.Text+"课程已经成功添加","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
					return;
				}
				catch(Exception express)
				{
					MessageBox.Show(express.ToString(),"信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
					return;
				}
			}
			else
			{
				MessageBox.Show("课程或班级名称为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Question);
				return;
			}
		}
		//获取班级编号
		private string GetClassID()
		{
			string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(strConn);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select 班级编号 from 班级 where (班级名称='"+txtClass.Text.Trim()+"')";
			object classID=cmd.ExecuteScalar();
			conn.Close();
			return classID.ToString();

		}
		//获取课程编号
		private string GetCourseID()
		{
			string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(strConn);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select 编号 from 课程表 where (名称='"+this.cobCourse.Text.Trim()+"')";
			object courseID=cmd.ExecuteScalar();
			conn.Close();
			return courseID.ToString();

		}
		//获取最大学号
		private string GetMaxPID()
		{
			string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(StrConn);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select max(学号) 最大学号 from 成绩表";
			int PerID=0;
			object result=cmd.ExecuteScalar();
			if(result!=System.DBNull.Value)
			{
				PerID=Convert.ToInt32(result)+1;
			}
			return PerID.ToString();
		}
		//获取成绩表最大序号
		private string GetMaxID()
		{
			string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(StrConn);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select max(序号) 最大序号 from 成绩表";
			int maxID=0;
			object result=cmd.ExecuteScalar();
			if(result!=System.DBNull.Value)
			{
				maxID=Convert.ToInt32(result)+1;
			}
			int length=maxID.ToString().Length;
			switch(length)
			{
				case 1:
					newID="000000"+maxID.ToString();
					break;
				case 2:
					newID="00000"+maxID.ToString();
					break;
				case 3:
					newID="0000"+maxID.ToString();
					break;
				case 4:
					newID="000"+maxID.ToString();
					break;
				case 5:
					newID="00"+maxID.ToString();
					break;
				case 6:
					newID="0"+maxID.ToString();
					break;
				case 7:
					newID=maxID.ToString();
					break;
			}
			return newID.ToString();
		}

		private void btnQuit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}

⌨️ 快捷键说明

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