欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现简单学生信息管理系统

程序员文章站 2022-05-26 14:58:03
 本文实例为大家分享了c#实现简单学生信息管理系统的具体代码,供大家参考,具体内容如下 一、运行环境windows,使用vs编译软件 二、主要功能 登录...

一、运行环境windows,使用vs编译软件

C#实现简单学生信息管理系统

二、主要功能

登录、添加学生信息、修改学生信息、删除学生信息、查询学生信息

三、实现步骤

1、登陆界面功能实现

老规矩,先贴下主要代码:

 //构造方法
 public login()
 {
 initializecomponent();
 this.label3.parent = this;
 this.label1.backcolor = color.transparent;
 this.label2.backcolor = color.transparent;
 this.label3.backcolor = color.transparent;
 this.formborderstyle = system.windows.forms.formborderstyle.none;
 }
 //对登录按钮的事件实现代码
 private void button1_click(object sender, eventargs e)
 {
 string ss = configurationmanager.connectionstrings["stu"].tostring();
 sqlconnection conn = new sqlconnection(ss);
 try
 {
 //开启连接 
 conn.open(); 
 // messagebox.show("数据库连接成功!"); 
 }
 catch (exception)
 {
 //messagebox.show("数据库连接失败!");
 } 
 string uname = txtname.text.trim();
 string pword = txtpass.text.trim();
 if(uname == ""|| pword == "")
 {
 messagebox.show("请输入用户名或密码!");
 return;
 }
 else
 {
 sqlcommand cmd = conn.createcommand();
 sqldataadapter adp = new sqldataadapter();
 string sql2 = "select * from account where name='"+ uname + " 'and pass='" + pword + " ' ";
 cmd.commandtext = sql2;
 adp.selectcommand = cmd;
 dataset dat = new dataset();
 adp.fill(dat, "account");
 if(dat.tables["account"].rows.count == 0)
 {
 messagebox.show("用户名或密码错误!");
 return;
 }
 else
 {
 form1.islogin = true;
 form1.username = this.txtname.text;
 this.close();
 }
 cmd.clone();
 }
 conn.close();
 
 }
 //实现按esc键关闭该窗口
 protected override bool processcmdkey(ref system.windows.forms.message msg, system.windows.forms.keys keydata)
 {
 switch (keydata)
 {
 case keys.escape:
 this.close();//esc关闭窗体
 break;
 }
 return false;
 }
 //重置按钮清空文本框
 private void button2_click(object sender, eventargs e)
 {
 txtname.text = "";
 txtpass.text = "";
 } 
 //密码输入完成后实现敲击enter键触发登录按钮
 private void txtpass_keydown(object sender, keyeventargs e)
 {
 if(e.keycode == keys.enter)
 {
 this.button1_click(sender, e);
 }
 }
 //实现按enter键使焦点转移到下一文本框,与tab键通用
 private void txtname_keypress(object sender, keypresseventargs e)
 {
 if (e.keychar == (char)keys.enter)
 {
 sendkeys.send("{tab}");
 }
 }

效果如下图:

C#实现简单学生信息管理系统

2、主界面功能实现

主要代码如下:

public static bool islogin = false;
 int id;
 sqlconnection conn;
 sqlcommand cmd;
 sqldataadapter adp;
 dataset dat;
 public static string username
 {
 get;
 set;
 }
 public form1()
 {
 initializecomponent();
 this.label1.backcolor = color.transparent;
 this.groupbox1.backcolor = color.transparent;
 this.groupbox2.backcolor = color.transparent;
 this.datagridview1.borderstyle = borderstyle.none;
 this.datagridview1.cellborderstyle = datagridviewcellborderstyle.none;
 
 this.formborderstyle = system.windows.forms.formborderstyle.none;
 this.text += "| 管理员:" + username;
 string ss = configurationmanager.connectionstrings["stu"].tostring();
 //建立数据库连接
 conn = new sqlconnection(ss);
 try
 {
 //开启连接 
 conn.open();
 // messagebox.show("数据库连接成功!"); 
 }
 catch (exception)
 {
 messagebox.show("数据库连接失败!");
 }
 
 }

 private void form1_load(object sender, eventargs e)
 {
 datagridviewdataload();
 
 //this.stu_denttableadapter.fill(this.students.stu_dent);

 }

 private void datagridviewdataload()
 {
 string sql1 = "select * from stu_dent";
 adp = new sqldataadapter(sql1,conn);
 dat = new dataset();
 adp.fill(dat);
 datagridview1.datasource = dat.tables[0];
 }

 private void button5_click(object sender, eventargs e)
 {
 string num = textbox1.text.trim();
 string name = textbox2.text.trim();
 string sql4 = "select * from stu_dent where 1=1";
 if(!string.isnullorempty(num))
 {
 sql4 += " and stunum=" + num;
 }
 if(!string.isnullorempty(name))
 {
 sql4 += " and stuname like '%" + name + "%'";
 }
 adp = new sqldataadapter(sql4, conn);
 dat = new dataset();
 adp.fill(dat);
 datagridview1.datasource = dat.tables[0];
 
 }
 protected override bool processcmdkey(ref system.windows.forms.message msg, system.windows.forms.keys keydata)
 {
 switch (keydata)
 {
 case keys.escape:
 this.close();//esc关闭窗体
 break;
 }
 return false;
 }
 private void button1_click(object sender, eventargs e)
 {
 addstudent addstudent = new addstudent();
 addstudent.startposition = formstartposition.centerscreen;
 //addstudent.show();
 addstudent.showdialog();
 datagridviewdataload();
 }

 private void button4_click(object sender, eventargs e)
 {
 this.close();
 }

 private void button2_click(object sender, eventargs e)
 {
 messagebox.show("请在学生信息显示框中双击所要删除学生所在的那一行即可!!!","提示");
 }
 private void button3_click(object sender, eventargs e)
 {
 messagebox.show("请在学生信息显示框中单击所要删修改学生所在的那一行的任意文字区域即可!", "提示");
 }
 private void datagridview1_celldoubleclick(object sender, datagridviewcelleventargs e)
 {
 dialogresult result = messagebox.show("确定删除该学生信息?", "删除", messageboxbuttons.okcancel);
 if(result == dialogresult.ok)
 {
 id = convert.toint32(datagridview1.rows[e.rowindex].cells[0].value);
 console.writeline(id);
 string sql2 = "delete from stu_dent where id=" + id;
 cmd = new sqlcommand(sql2, conn);
 cmd.executenonquery();

 }
 datagridviewdataload();
 }

 private void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e)
 {
 id = convert.toint32(this.datagridview1.rows[e.rowindex].cells[0].value);
 updatestudent updatestudent = new updatestudent(id);
 updatestudent.startposition = formstartposition.centerscreen;
 updatestudent.showdialog();
 datagridviewdataload();
 }

效果如下:

C#实现简单学生信息管理系统

3、添加学生信息功能实现

主要代码如下:

public addstudent()
 {
 initializecomponent();
 this.formborderstyle = system.windows.forms.formborderstyle.none;
 string ss = configurationmanager.connectionstrings["stu"].tostring();
 //建立数据库连接
 conn = new sqlconnection(ss);
 try
 {
 //开启连接 
 conn.open();
 // messagebox.show("数据库连接成功!"); 
 }
 catch (exception)
 {
 messagebox.show("数据库连接失败!");
 }

 }

 private void addstudent_load(object sender, eventargs e)
 {
 
 }

 private void label1_click(object sender, eventargs e)
 {

 }
 protected override bool processcmdkey(ref system.windows.forms.message msg, system.windows.forms.keys keydata)
 {
 switch (keydata)
 {
 case keys.escape:
 this.close();//esc关闭窗体
 break;
 }
 return false;
 }
 private void button1_click(object sender, eventargs e)
 {
 string stunum = textbox1.text.trim();
 string stuname = textbox2.text.trim();
 int stuage;
 int32.tryparse(textbox3.text.trim(), out stuage);
 string stuclass = textbox5.text.trim();
 string stuphone = textbox6.text.trim();
 string stusex = radiobutton1.checked ? "男" : "女";
 if (string.isnullorempty(stunum))
 {
 messagebox.show("学号不能为空!");
 }
 if (string.isnullorempty(stuname))
 {
 messagebox.show("姓名不能为空!");
 }
 
 if (string.isnullorempty(stuclass))
 {
 messagebox.show("班级不能为空!");
 }
 if (string.isnullorempty(stuphone))
 {
 messagebox.show("联系方式不能为空!");
 }
 string sql = string.format("insert into stu_dent values ('{0}','{1}','{2}','{3}','{4}','{5}')", stunum, stuname,stuage,stusex, stuclass, stuphone );
 cmd = new sqlcommand(sql, conn);
 int count = cmd.executenonquery();
 if (count > 0)
 {
 messagebox.show("添加成功!");
 }
 else
 {
 messagebox.show("添加失败!");
 }
 this.close();
 }

C#实现简单学生信息管理系统 

4、删除学生信息功能实现

在这里采用双击所要删除学生所在的那一行的任意位置即可

主要代码如下:

private void datagridview1_celldoubleclick(object sender, datagridviewcelleventargs e)
 {
 dialogresult result = messagebox.show("确定删除该学生信息?", "删除", messageboxbuttons.okcancel);
 if(result == dialogresult.ok)
 {
 id = convert.toint32(datagridview1.rows[e.rowindex].cells[0].value);
 console.writeline(id);
 string sql2 = "delete from stu_dent where id=" + id;
 cmd = new sqlcommand(sql2, conn);
 cmd.executenonquery();

 }
 datagridviewdataload();
 }

效果如下图(双击第一行进行删除):

删除前:

C#实现简单学生信息管理系统

删除后:

C#实现简单学生信息管理系统

5、修改学生信息功能实现

在这里采用单击所要修改学生所在行任意文字处即可

主要代码如下:

public updatestudent(int id)
 {
 this.id = id;
 string ss = configurationmanager.connectionstrings["stu"].tostring();
 conn = new sqlconnection(ss);
 try
 {
 //开启连接 
 conn.open();
 // messagebox.show("数据库连接成功!"); 
 }
 catch (exception)
 {
 messagebox.show("数据库连接失败!");
 }
 initializecomponent();
 this.formborderstyle = system.windows.forms.formborderstyle.none;
 }
 protected override bool processcmdkey(ref system.windows.forms.message msg, system.windows.forms.keys keydata)
 {
 switch (keydata)
 {
 case keys.escape:
 this.close();//esc关闭窗体
 break;
 }
 return false;
 }
 private void updatestudent_load(object sender, eventargs e)
 {
 cmd = conn.createcommand();
 string sql1 = "select * from stu_dent where id=" + id;
 cmd.commandtext = sql1;
 reader = cmd.executereader();
 if(reader.hasrows)
 {
 reader.read();
 textbox1.text = reader.getstring(1);
 textbox2.text = reader.getstring(2);
 textbox3.text = reader.getint32(3).tostring();
 if(reader.getstring(4) == "男")
 {
 this.radiobutton2.checked = true;
 }
 else
 {
 this.radiobutton1.checked = true;
 }
 textbox5.text = reader.getstring(5);
 textbox6.text = reader.getstring(6);
 }

 reader.close();
 
 }

 private void button1_click(object sender, eventargs e)
 {
 string stunum = textbox1.text.trim();
 string stuname = textbox2.text.trim();
 int stuage;
 int32.tryparse(textbox3.text.trim(), out stuage);
 string stuclass = textbox5.text.trim();
 string stuphone = textbox6.text.trim();
 string stusex = radiobutton1.checked ? "男" : "女";
 if (string.isnullorempty(stuname))
 {
 messagebox.show("姓名不能为空!");
 }

 if (string.isnullorempty(stuclass))
 {
 messagebox.show("班级不能为空!");
 }
 if (string.isnullorempty(stuphone))
 {
 messagebox.show("联系方式不能为空!");
 }
 string sql = string.format("update stu_dent set stuname='{0}',stuage={1},stusex='{2}',stuclass='{3}',stuphone='{4}' where stunum='{5}'", stuname, stuage, stusex, stuclass, stuphone, stunum);
 cmd = new sqlcommand(sql, conn);
 int count = cmd.executenonquery();
 if (count > 0)
 {
 messagebox.show("修改成功!");
 }
 else
 {
 messagebox.show("修改失败!");
 }
 this.close();
 }

 private void button2_click(object sender, eventargs e)
 {
 cmd = conn.createcommand();
 string sql1 = "select * from stu_dent where id=" + id;
 cmd.commandtext = sql1;
 reader = cmd.executereader();
 if (reader.hasrows)
 {
 reader.read();
 textbox1.text = reader.getstring(1);
 textbox2.text = reader.getstring(2);
 textbox3.text = reader.getint32(3).tostring();
 if (reader.getstring(4) == "男")
 {
 this.radiobutton2.checked = true;
 }
 else
 {
 this.radiobutton1.checked = true;
 }
 textbox5.text = reader.getstring(5);
 textbox6.text = reader.getstring(6);
 }

 reader.close();
 }

(在这里将郭某某的专业班级为例)

修改前:

C#实现简单学生信息管理系统

修改后:

C#实现简单学生信息管理系统

6、查询学生信息功能实现

查询功能就写了两种查询方式

主要代码如下:

private void button5_click(object sender, eventargs e)
 {
 string num = textbox1.text.trim();
 string name = textbox2.text.trim();
 string sql4 = "select * from stu_dent where 1=1";
 if(!string.isnullorempty(num))
 {
 sql4 += " and stunum=" + num;
 }
 if(!string.isnullorempty(name))
 {
 sql4 += " and stuname like '%" + name + "%'";
 }
 adp = new sqldataadapter(sql4, conn);
 dat = new dataset();
 adp.fill(dat);
 datagridview1.datasource = dat.tables[0];
 
 }

效果如下:

按学号查询:

C#实现简单学生信息管理系统

按姓名查询:

C#实现简单学生信息管理系统

总结:这是上完c#课后,自己无聊整着玩的,功能实现简单易懂,比较容易,希望能对你的学习有所帮助。对了,每个窗体的背景图片是我再网上随便找的,你可以选择你喜欢的背景。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。