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

📄 querystu.cs

📁 c#+SQL Server 中小型信息系统开发实例精选书上代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace StudentManagementsystem
{
	/// <summary>
	/// QueryStu 的摘要说明。
	/// </summary>
	public class QueryStu : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox QryStuName;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button ExitQryStu;
		private System.Windows.Forms.DataGrid QryStuDg;
		private System.Windows.Forms.Button QryStuN;
		private System.Windows.Forms.Button QueryStuClass;
		private System.Windows.Forms.ComboBox ClassList;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		/// 
		private string QryStustrconn;
		private string QryStuconnstr="workstation id=localhost;Integrated Security=SSPI;database=StuMagSys";
		private SqlDataAdapter QryStusqlda;

		private SqlConnection QrySconn;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;

		private System.ComponentModel.Container components = null;

		public QueryStu()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			this.QueryStu_Load();
			//
			// 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.QryStuName = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.QryStuDg = new System.Windows.Forms.DataGrid();
			this.QryStuN = new System.Windows.Forms.Button();
			this.ExitQryStu = new System.Windows.Forms.Button();
			this.QueryStuClass = new System.Windows.Forms.Button();
			this.ClassList = new System.Windows.Forms.ComboBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			((System.ComponentModel.ISupportInitialize)(this.QryStuDg)).BeginInit();
			this.SuspendLayout();
			// 
			// QryStuName
			// 
			this.QryStuName.Location = new System.Drawing.Point(160, 24);
			this.QryStuName.Name = "QryStuName";
			this.QryStuName.Size = new System.Drawing.Size(144, 21);
			this.QryStuName.TabIndex = 0;
			this.QryStuName.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(48, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "按学生姓名检索:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(48, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "按班级检索:";
			// 
			// QryStuDg
			// 
			this.QryStuDg.DataMember = "";
			this.QryStuDg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.QryStuDg.Location = new System.Drawing.Point(40, 120);
			this.QryStuDg.Name = "QryStuDg";
			this.QryStuDg.Size = new System.Drawing.Size(504, 256);
			this.QryStuDg.TabIndex = 4;
			// 
			// QryStuN
			// 
			this.QryStuN.Location = new System.Drawing.Point(320, 24);
			this.QryStuN.Name = "QryStuN";
			this.QryStuN.Size = new System.Drawing.Size(96, 24);
			this.QryStuN.TabIndex = 5;
			this.QryStuN.Text = "检索";
			this.QryStuN.Click += new System.EventHandler(this.QryStuN_Click);
			// 
			// ExitQryStu
			// 
			this.ExitQryStu.Location = new System.Drawing.Point(448, 48);
			this.ExitQryStu.Name = "ExitQryStu";
			this.ExitQryStu.Size = new System.Drawing.Size(96, 24);
			this.ExitQryStu.TabIndex = 6;
			this.ExitQryStu.Text = "退出";
			this.ExitQryStu.Click += new System.EventHandler(this.ExitQryStu_Click);
			// 
			// QueryStuClass
			// 
			this.QueryStuClass.Location = new System.Drawing.Point(320, 56);
			this.QueryStuClass.Name = "QueryStuClass";
			this.QueryStuClass.Size = new System.Drawing.Size(96, 24);
			this.QueryStuClass.TabIndex = 7;
			this.QueryStuClass.Text = "检索";
			this.QueryStuClass.Click += new System.EventHandler(this.QueryStuClass_Click);
			// 
			// ClassList
			// 
			this.ClassList.Location = new System.Drawing.Point(160, 56);
			this.ClassList.Name = "ClassList";
			this.ClassList.Size = new System.Drawing.Size(144, 20);
			this.ClassList.TabIndex = 8;
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(32, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(520, 80);
			this.groupBox1.TabIndex = 9;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "检索项";
			// 
			// groupBox2
			// 
			this.groupBox2.Location = new System.Drawing.Point(32, 96);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(520, 288);
			this.groupBox2.TabIndex = 10;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "检索结果";
			// 
			// QueryStu
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(576, 397);
			this.Controls.Add(this.ClassList);
			this.Controls.Add(this.QueryStuClass);
			this.Controls.Add(this.ExitQryStu);
			this.Controls.Add(this.QryStuN);
			this.Controls.Add(this.QryStuDg);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.QryStuName);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox2);
			this.Name = "QueryStu";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "学生信息检索";
			((System.ComponentModel.ISupportInitialize)(this.QryStuDg)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
		//检索数据库表class,并把检索结果填充到班级列表
		private void QueryStu_Load()
		{  

			DataSet QryStudClassset=new DataSet();
			
			QryStustrconn="select Class_name from class";
			this.QrySconn=new SqlConnection(this.QryStuconnstr);
			this.QryStusqlda=new SqlDataAdapter(this.QryStustrconn,this.QrySconn);
			QryStudClassset.Clear();
			this.QryStusqlda.Fill(QryStudClassset,"Class");
			for(int i=0;i<QryStudClassset.Tables[0].Rows.Count;i++)
			{
				ClassList.Items.Add(QryStudClassset.Tables[0].Rows[i][0]);
			}
			ClassList.SelectedIndex=0;
		}
		//按照学生姓名检索学生信息
		private void QryStuN_Click(object sender, System.EventArgs e)
		{
			try
			{
				DataSet QryStudset =new DataSet();
				QryStustrconn="select * from student where student_name="+"'"+this.QryStuName.Text.ToString().Trim()+"'";
				QrySconn=new SqlConnection(QryStuconnstr);
				QryStusqlda=new SqlDataAdapter(QryStustrconn,QrySconn);
				QryStudset.Clear();

				QryStusqlda.Fill(QryStudset,"Student");
				QryStuDg.DataSource=QryStudset.Tables[0];
		
			}
			catch
			{
				MessageBox.Show("检索学生信息失败。","信息");
			}
		}
		
		private void ExitQryStu_Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("您确认要退出?","确认",MessageBoxButtons.YesNo)==DialogResult.Yes)
				this.Close();
		}
		//按照学生所在的班级检索学生信息
		private void QueryStuClass_Click(object sender, System.EventArgs e)
		{
			try
			{
				DataSet QryStudset =new DataSet();
				QryStustrconn="select Student_id as 学号,Student_name as 姓名,Sex as 性别,Birth as 出生年月,Nation as 民族,student.Class_id as 班级,Entrance_date as 入学时间,home as 家庭地址,politic as 政治面貌,ID as 身份证号,Job as 职位,specialty as 所学专业 from student,class where class.class_id=student.class_id and class.Class_name="+"'"+this.ClassList.SelectedItem.ToString().Trim()+"'";
				QrySconn=new SqlConnection(QryStuconnstr);
				QryStusqlda=new SqlDataAdapter(QryStustrconn,QrySconn);
				QryStudset.Clear();

				QryStusqlda.Fill(QryStudset,"Class_id");
				QryStuDg.DataSource=QryStudset.Tables[0];
			}
			catch
			{
				MessageBox.Show("检索学生信息失败。","信息");
			}
		}


	}
}

⌨️ 快捷键说明

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