stu.cs

来自「这是用SQL数据库做的学生信息管理系统。简明」· CS 代码 · 共 219 行

CS
219
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 学生与成绩
{
	/// <summary>
	/// Stu 的摘要说明。
	/// </summary>
	public class Stu : System.Windows.Forms.Form
	{
		private DataSet ds=new DataSet();
		private LinkDataBase link=new LinkDataBase();
		private string sendStrSQL;
		private string sendTableName="STU";
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cobClass;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtID;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Button btnSearch;
		private System.Windows.Forms.DataGrid dataGrid1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Stu()
		{
			//
			// 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.cobClass = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtID = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.btnSearch = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(15, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "选择班级";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// cobClass
			// 
			this.cobClass.Location = new System.Drawing.Point(92, 27);
			this.cobClass.Name = "cobClass";
			this.cobClass.Size = new System.Drawing.Size(121, 20);
			this.cobClass.TabIndex = 1;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(234, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "学号";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtID
			// 
			this.txtID.Location = new System.Drawing.Point(303, 26);
			this.txtID.Name = "txtID";
			this.txtID.TabIndex = 3;
			this.txtID.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(424, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 23);
			this.label3.TabIndex = 4;
			this.label3.Text = "姓名";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(501, 26);
			this.txtName.Name = "txtName";
			this.txtName.TabIndex = 5;
			this.txtName.Text = "";
			// 
			// btnSearch
			// 
			this.btnSearch.Location = new System.Drawing.Point(622, 24);
			this.btnSearch.Name = "btnSearch";
			this.btnSearch.TabIndex = 6;
			this.btnSearch.Text = "查询";
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(20, 72);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(672, 376);
			this.dataGrid1.TabIndex = 7;
			// 
			// Stu
			// 
			this.AcceptButton = this.btnSearch;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(712, 466);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.btnSearch);
			this.Controls.Add(this.txtName);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.txtID);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.cobClass);
			this.Controls.Add(this.label1);
			this.Name = "Stu";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "班级情况查询";
			this.Load += new System.EventHandler(this.Stu_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Stu_Load(object sender, System.EventArgs e)
		{
			this.dataGrid1.CaptionText="班级学生情况查询";
			//获取班级名称下拉列表
			string StrSQL="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(StrSQL);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select distinct 班级名称 from 班级";
			SqlDataReader dr=cmd.ExecuteReader();
			while(dr.Read())
			{
				this.cobClass.Items.Add(dr.GetValue(0).ToString().Trim());
			}
		}

		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			//产生空表
			if(this.cobClass.Text=="")
			{
				this.sendStrSQL="select 学号,姓名,性别,出生 as 出生日期,班级编号 as 班级,地址,电话,手机,毕业否 from STU"
					+" where(1=2)";
				ds=link.SelectDataBase(sendStrSQL,sendTableName);
				this.dataGrid1.DataSource=ds;
				this.dataGrid1.DataMember="STU";
			}
			else
			{
				string classID=GetClassID();
				this.sendStrSQL="SELECT  stu.学号, STU.姓名, STu.性别,STU.出生 AS 出生日期,班级.班级名称 AS 班级,STU.地址,STU.电话,STU.手机,STU.毕业否 FROM STU INNER JOIN 班级 ON STU.班级编号 = 班级.班级编号 "
					+" where (STU.班级编号='"+classID.Trim()+"') AND (STU.学号 like'%"+txtID.Text.Trim()
					+"%') and (STU.姓名 LIKE '%"+txtName.Text.Trim()+"%')";
				this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
				this.dataGrid1.DataSource=ds;
				this.dataGrid1.DataMember="STU";
			}

		}
		//班级名称和班级编号的相互转换
		private string GetClassID()
		{
			string StrSQL="workstation id=localhost;Integrated Security=SSPI;database=stu01";
			SqlConnection conn=new SqlConnection(StrSQL);
			conn.Open();
			SqlCommand cmd=conn.CreateCommand();
			cmd.CommandText="select distinct 班级编号 from 班级 where(班级名称='"+this.cobClass.Text.Trim()+"')";
			object classID=cmd.ExecuteScalar();
			return classID.ToString();
		}
	}
}

⌨️ 快捷键说明

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