📄 updatestu.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>
/// UpdateStu 的摘要说明。
/// </summary>
public class UpdateStu : System.Windows.Forms.Form
{
private System.Windows.Forms.Button QueryStuClass;
private System.Windows.Forms.Button QryStuN;
private System.Windows.Forms.DataGrid UpdStuGrdDg;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox QryStuName;
private System.Windows.Forms.Button SaveChg;
private System.Windows.Forms.Button ExitUpdStu;
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private string UpdStustrconnName;
private string UpdStustrconnClass;
private SqlConnection UpdSconn;
private string UpdStuconnstr="workstation id=localhost;Integrated Security=SSPI;database=StuMagSys";
private SqlDataAdapter UpdStuGrdsqlda;
private DataSet UpdClassStudset =new DataSet();
private DataSet UpdNameStudset =new DataSet();
private SqlCommandBuilder SqlComm;
private string initStringSql="select Student_id as 学号,Student_name as 姓名,Sex as 性别,Birth as 出生年月,Nation as 民族,Class_id as 班级,Entrance_date as 入学时间,home as 家庭地址,politic as 政治面貌,ID as 身份证号,Job as 职位,specialty as 所学专业 from Student";
private System.Windows.Forms.Button DelRecord;
private System.Windows.Forms.ComboBox QryStuCla;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.ComponentModel.Container components = null;
public UpdateStu()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.UpdSconn=new SqlConnection(UpdStuconnstr);
this.UpdStuGrdsqlda=new SqlDataAdapter(initStringSql,UpdSconn);
this.UpdClassStudset.Clear();
this.UpdNameStudset.Clear();
this.UpdStuGrdsqlda.Fill(UpdClassStudset,"Student");
this.UpdStuGrdDg.DataSource=UpdClassStudset.Tables[0];
this.UpdStuGrdsqlda.Fill(UpdNameStudset,"Student");
this.UpdateStu_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.QueryStuClass = new System.Windows.Forms.Button();
this.ExitUpdStu = new System.Windows.Forms.Button();
this.QryStuN = new System.Windows.Forms.Button();
this.UpdStuGrdDg = new System.Windows.Forms.DataGrid();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.QryStuName = new System.Windows.Forms.TextBox();
this.SaveChg = new System.Windows.Forms.Button();
this.DelRecord = new System.Windows.Forms.Button();
this.QryStuCla = new System.Windows.Forms.ComboBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.UpdStuGrdDg)).BeginInit();
this.SuspendLayout();
//
// QueryStuClass
//
this.QueryStuClass.Location = new System.Drawing.Point(392, 56);
this.QueryStuClass.Name = "QueryStuClass";
this.QueryStuClass.Size = new System.Drawing.Size(96, 24);
this.QueryStuClass.TabIndex = 23;
this.QueryStuClass.Text = "检索";
this.QueryStuClass.Click += new System.EventHandler(this.QueryStuClass_Click);
//
// ExitUpdStu
//
this.ExitUpdStu.Location = new System.Drawing.Point(368, 384);
this.ExitUpdStu.Name = "ExitUpdStu";
this.ExitUpdStu.Size = new System.Drawing.Size(96, 24);
this.ExitUpdStu.TabIndex = 22;
this.ExitUpdStu.Text = "退出";
this.ExitUpdStu.Click += new System.EventHandler(this.ExitUpdStu_Click);
//
// QryStuN
//
this.QryStuN.Location = new System.Drawing.Point(392, 24);
this.QryStuN.Name = "QryStuN";
this.QryStuN.Size = new System.Drawing.Size(96, 24);
this.QryStuN.TabIndex = 21;
this.QryStuN.Text = "检索";
this.QryStuN.Click += new System.EventHandler(this.QryStuN_Click);
//
// UpdStuGrdDg
//
this.UpdStuGrdDg.DataMember = "";
this.UpdStuGrdDg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.UpdStuGrdDg.Location = new System.Drawing.Point(60, 112);
this.UpdStuGrdDg.Name = "UpdStuGrdDg";
this.UpdStuGrdDg.Size = new System.Drawing.Size(520, 256);
this.UpdStuGrdDg.TabIndex = 20;
//
// label2
//
this.label2.Location = new System.Drawing.Point(120, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 19;
this.label2.Text = "按班级检索:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(120, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 17;
this.label1.Text = "按学生姓名检索:";
//
// QryStuName
//
this.QryStuName.Location = new System.Drawing.Point(232, 24);
this.QryStuName.Name = "QryStuName";
this.QryStuName.Size = new System.Drawing.Size(144, 21);
this.QryStuName.TabIndex = 16;
this.QryStuName.Text = "";
//
// SaveChg
//
this.SaveChg.Location = new System.Drawing.Point(160, 384);
this.SaveChg.Name = "SaveChg";
this.SaveChg.Size = new System.Drawing.Size(96, 24);
this.SaveChg.TabIndex = 25;
this.SaveChg.Text = "保存修改";
this.SaveChg.Click += new System.EventHandler(this.SaveChg_Click);
//
// DelRecord
//
this.DelRecord.Location = new System.Drawing.Point(264, 384);
this.DelRecord.Name = "DelRecord";
this.DelRecord.Size = new System.Drawing.Size(96, 24);
this.DelRecord.TabIndex = 24;
this.DelRecord.Text = "删除该记录";
this.DelRecord.Click += new System.EventHandler(this.DelRecord_Click);
//
// QryStuCla
//
this.QryStuCla.Location = new System.Drawing.Point(232, 56);
this.QryStuCla.Name = "QryStuCla";
this.QryStuCla.Size = new System.Drawing.Size(144, 20);
this.QryStuCla.TabIndex = 26;
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(48, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(544, 80);
this.groupBox1.TabIndex = 27;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "检索项";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(48, 88);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(544, 288);
this.groupBox2.TabIndex = 28;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "检索结果";
//
// UpdateStu
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 421);
this.Controls.Add(this.QryStuCla);
this.Controls.Add(this.SaveChg);
this.Controls.Add(this.DelRecord);
this.Controls.Add(this.QueryStuClass);
this.Controls.Add(this.ExitUpdStu);
this.Controls.Add(this.QryStuN);
this.Controls.Add(this.UpdStuGrdDg);
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 = "UpdateStu";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "更新学生信息";
((System.ComponentModel.ISupportInitialize)(this.UpdStuGrdDg)).EndInit();
this.ResumeLayout(false);
}
#endregion
//在数据库中检索班级名称并填充到班级列表
private void UpdateStu_Load()
{
DataSet UpdStudClaset=new DataSet();
initStringSql="select Class_name from class";
this.UpdSconn=new SqlConnection(this.UpdStuconnstr);
this.UpdStuGrdsqlda=new SqlDataAdapter(this.initStringSql,this.UpdSconn);
UpdStudClaset.Clear();
this.UpdStuGrdsqlda.Fill(UpdStudClaset,"Class");
for(int i=0;i<UpdStudClaset.Tables[0].Rows.Count;i++)
{
QryStuCla.Items.Add(UpdStudClaset.Tables[0].Rows[i][0]);
}
QryStuCla.SelectedIndex=0;
}
private void ExitUpdStu_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("您确认要退出本系统?","确认",MessageBoxButtons.YesNoCancel)==DialogResult.Yes)
this.Close();
}
//根据学生姓名查询学生信息
private void QryStuN_Click(object sender, System.EventArgs e)
{
try
{
UpdStustrconnName="select Student_id as 学号,Student_name as 姓名,Sex as 性别,Birth as 出生年月,Nation as 民族,Class_id as 班级,Entrance_date as 入学时间,home as 家庭地址,politic as 政治面貌,ID as 身份证号,Job as 职位,specialty as 所学专业 from student where student_name="+"'"+this.QryStuName.Text.ToString().Trim()+"'";
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(UpdStustrconnName,UpdSconn);
UpdNameStudset.Clear();
UpdStuGrdsqlda.Fill(UpdNameStudset,"student");
UpdStuGrdDg.DataSource=UpdNameStudset.Tables[0];
}
catch
{
MessageBox.Show("查询失败。","确认");
}
}
//根据学生班级查询学生信息
private void QueryStuClass_Click(object sender, System.EventArgs e)
{
try
{
UpdStustrconnClass="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.QryStuCla.SelectedItem.ToString().Trim()+"'";
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(UpdStustrconnClass,UpdSconn);
UpdClassStudset.Clear();
UpdStuGrdsqlda.Fill(UpdClassStudset,"student");
UpdStuGrdDg.DataSource=UpdClassStudset.Tables[0];
}
catch
{
MessageBox.Show("查询失败。","确认");
}
}
//保存修改
private void SaveChg_Click(object sender, System.EventArgs e)
{
try
{
int row=this.UpdStuGrdDg.CurrentCell.RowNumber;
this.UpdStuGrdDg.CurrentCell=new DataGridCell(row+1,0);
if(UpdClassStudset.HasChanges())//如果改动是在根据学生班级检索的结果集上进行的那么,更改该结果集
{
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(this.UpdStustrconnClass,this.UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
this.UpdStuGrdsqlda.Update(this.UpdClassStudset.GetChanges(),"student");
MessageBox.Show("保存修改成功!","信息");
}
else if(UpdNameStudset.HasChanges())//如果改动是在根据学生姓名检索的结果集上进行的那么,更改该结果集
{
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(this.UpdStustrconnName,UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
UpdStuGrdsqlda.Update(this.UpdNameStudset.GetChanges(),"student");
MessageBox.Show("保存修改成功!","信息");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"信息");
}
}
//删除学生记录
private void DelRecord_Click(object sender, System.EventArgs e)
{
int rowNumber=this.UpdStuGrdDg.CurrentCell.RowNumber;
try
{
this.UpdClassStudset.Tables[0].Rows[rowNumber].Delete();
this.UpdNameStudset.Tables[0].Rows[rowNumber].Delete();
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter("",UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
UpdStuGrdsqlda.Update(this.UpdNameStudset.GetChanges(),"student");
UpdStuGrdsqlda.Update(this.UpdClassStudset.GetChanges(),"student");
MessageBox.Show("删除成功!","信息");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"信息");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -