📄 stu2.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>
/// Stu2 的摘要说明。
/// </summary>
public class Stu2 : System.Windows.Forms.Form
{
public static string UserID,ClassID;
private DataSet ds=new DataSet();
private LinkDataBase link=new LinkDataBase();
private string sendStrSQL;
private string sendTableName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox comboClass;
private System.Windows.Forms.ComboBox comboDepartment;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.SqlClient.SqlDataAdapter da1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private 学生与成绩.DataSet3 dataSet31;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Stu2()
{
//
// 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.label2 = new System.Windows.Forms.Label();
this.comboClass = new System.Windows.Forms.ComboBox();
this.comboDepartment = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet31 = new 学生与成绩.DataSet3();
this.da1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet31)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(333, 13);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "选择班级";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(39, 13);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "选择系部";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// comboClass
//
this.comboClass.Location = new System.Drawing.Point(438, 16);
this.comboClass.Name = "comboClass";
this.comboClass.Size = new System.Drawing.Size(216, 20);
this.comboClass.TabIndex = 2;
this.comboClass.SelectedIndexChanged += new System.EventHandler(this.comboClass_SelectedIndexChanged);
//
// comboDepartment
//
this.comboDepartment.Location = new System.Drawing.Point(144, 16);
this.comboDepartment.Name = "comboDepartment";
this.comboDepartment.Size = new System.Drawing.Size(184, 20);
this.comboDepartment.TabIndex = 3;
this.comboDepartment.SelectedIndexChanged += new System.EventHandler(this.comboDepartment_SelectedIndexChanged);
//
// dataGrid1
//
this.dataGrid1.DataMember = "STU";
this.dataGrid1.DataSource = this.dataSet31;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(30, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(632, 192);
this.dataGrid1.TabIndex = 4;
//
// dataSet31
//
this.dataSet31.DataSetName = "DataSet3";
this.dataSet31.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// da1
//
this.da1.SelectCommand = this.sqlSelectCommand1;
this.da1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "STU", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("学号", "学号"),
new System.Data.Common.DataColumnMapping("姓名", "姓名"),
new System.Data.Common.DataColumnMapping("性别", "性别"),
new System.Data.Common.DataColumnMapping("出生", "出生"),
new System.Data.Common.DataColumnMapping("班级编号", "班级编号"),
new System.Data.Common.DataColumnMapping("地址", "地址"),
new System.Data.Common.DataColumnMapping("电话", "电话"),
new System.Data.Common.DataColumnMapping("手机", "手机"),
new System.Data.Common.DataColumnMapping("毕业否", "毕业否")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 学号, 姓名, 性别, 出生, 班级编号, 地址, 电话, 手机, 毕业否 FROM STU";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=localhost;packet size=4096;integrated security=SSPI;data source=ys" +
"js44;persist security info=False;initial catalog=stu01";
//
// Stu2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(692, 246);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboDepartment);
this.Controls.Add(this.comboClass);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Stu2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "显示某班级学生情况";
this.Load += new System.EventHandler(this.Stu2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet31)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Stu2_Load(object sender, System.EventArgs e)
{
da1.Fill(dataSet31,"STU");
string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(StrConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select distinct 名称 from 系部";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.comboDepartment.Items.Add(dr.GetValue(0).ToString().Trim());
}
}
private void GetClass()
{
string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(StrConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
UserID=this.GetClassID();
cmd.CommandText="select distinct 班级名称 from 班级 where(系部编号='"+UserID.Trim()+"')";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.comboClass.Items.Add(dr.GetValue(0).ToString().Trim());
}
}
//系部名称和系部编号的相互转换
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(名称='"+this.comboDepartment.Text.Trim()+"')";
object UserID=cmd.ExecuteScalar();
return UserID.ToString();
}
//获取班级编号
private string GetID()
{
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.comboClass.Text.Trim()+"')";
object ClassID=cmd.ExecuteScalar();
return ClassID.ToString();
}
private void comboDepartment_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.comboDepartment.Text!="")
{
this.GetClass();
}
}
private void comboClass_SelectedIndexChanged(object sender, System.EventArgs e)
{
ClassID=this.GetID();
this.sendStrSQL="select * from STU where(班级编号='"+ClassID.Trim()+"')";
this.sendTableName="STU";
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="STU";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -