会搜索学生信息管理系统,想必兄台是个正在实训阶段的大学生吧。如果你的实训内容是用C#开发一个学生信息管理系统的话,那么这篇文章或许可以帮到你,说不好还可以让你的实训作品脱颖而出,让其他人眼前一亮。先看效果图,随后上码。
界面展示(部分)
源码展示(部分)
初始化类
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp205
{
public class LJ
{
//登陆时的账号
private static string zhangHao;
public static string ZhangHao { get => zhangHao; set => zhangHao = value; }
//标记加载哪种身份的数据
private static int login;
public static int Login { get => login; set => login = value; }
//数据库连接字符串
private static string constr;
public static string Constr { get => constr; set => constr = value; }
//数据备份与恢复的文件路径
private static string lu;
public static string Lu { get => lu; set => lu = value; }
//预览图片的路径
private static Byte[] imageByte;
public static byte[] ImageByte { get => imageByte; set => imageByte = value; }
//日期(用于弹窗)
private static string time;
public static string Time { get => time; set => time = value; }
//性别(用于弹窗)
private static string sex;
public static string Sex { get => sex; set => sex = value; }
//专业(用于弹窗)
private static string zhuanYe;
public static string ZhuanYe { get => zhuanYe; set => zhuanYe = value; }
//政治面貌(用于弹窗)
private static string zhengZhi;
public static string ZhengZhi { get => zhengZhi; set => zhengZhi = value; }
//民族(用于弹窗)
private static string minZhu;
public static string MinZhu { get => minZhu; set => minZhu = value; }
//全部数据表
public static DataTable Logindt = new DataTable();
public static DataTable Studentdt = new DataTable();
public static DataTable Teacherdt = new DataTable();
public static DataTable Leaderdt = new DataTable();
public static DataTable Classdt = new DataTable();
public static DataTable Scdt = new DataTable();
public static DataTable Bandt = new DataTable();
public static DataTable Zhuandt = new DataTable();
}
//用于泛型,方便加载数据
public class DL
{
private RadioButton radio;
public RadioButton Radio { get => radio; set => radio = value; }
private int t;
public int T { get => t; set => t = value; }
private Form form;
public Form Form { get => form; set => form = value; }
}
}
全局方法
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp205
{
class Class1
{
//加载不同表的数据
public static void connect(string t)
{
try
{
string sql = "select * from " + t;
SqlDataAdapter adapter = new SqlDataAdapter(sql, LJ.Constr);
if (t == "Student")
{
LJ.Studentdt.Clear();
adapter.Fill(LJ.Studentdt);
}
else if (t == "Class")
{
LJ.Classdt.Clear();
adapter.Fill(LJ.Classdt);
}
else if (t == "Teacher")
{
LJ.Teacherdt.Clear();
adapter.Fill(LJ.Teacherdt);
}
else if (t == "StuCla")
{
LJ.Scdt.Clear();
adapter.Fill(LJ.Scdt);
}
else if (t == "Ban")
{
LJ.Bandt.Clear();
adapter.Fill(LJ.Bandt);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//数据库备份与恢复
public static void suJu()
{
string conStr = @"server=LAPTOP-ILT6KA7J\SQLEXPRESS;database=master;user=sa;pwd=sa";
try
{
SqlConnection sql = new SqlConnection(conStr);
sql.Open();
SqlCommand command = new SqlCommand(LJ.Lu, sql);
command.ExecuteNonQuery();
sql.Close();
MessageBox.Show("操作成功", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public static void Dela()
{
string conStr = @"server=LAPTOP-ILT6KA7J\SQLEXPRESS;database=master;user=sa;pwd=sa";
try
{
SqlConnection sql = new SqlConnection(conStr);
sql.Open();
SqlCommand command = new SqlCommand("drop database [training]", sql);
command.ExecuteNonQuery();
sql.Close();
MessageBox.Show("操作成功", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
数据库连接
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp205
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
comboBox1.Items.Add(Environment.MachineName + @"\SQLEXPRESS");
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "" && textBox1.Text != "")
{
lianjieBtn.Enabled = true;
}
if (comboBox2.SelectedIndex == 0)
{
comboBox3.Enabled = false;
textBox1.Enabled = false;
}
else
{
comboBox3.Enabled = true;
textBox1.Enabled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length < 1)
{
errorProvider1.SetError(textBox1, "禁止为空");
lianjieBtn.Enabled = false;
}
else
{
errorProvider1.Clear();
if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "" && textBox1.Text != "")
{
lianjieBtn.Enabled = true;
}
}
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (comboBox1.Text.Length < 1)
{
errorProvider2.SetError(comboBox1, "禁止为空");
lianjieBtn.Enabled = false;
}
else
{
errorProvider2.Clear();
if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "" && textBox1.Text != "")
{
lianjieBtn.Enabled = true;
}
}
}
private void comboBox3_TextChanged(object sender, EventArgs e)
{
if (comboBox3.Text.Length < 1)
{
errorProvider3.SetError(comboBox3, "禁止为空");
lianjieBtn.Enabled = false;
}
else
{
errorProvider3.Clear();
if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "" && textBox1.Text != "")
{
lianjieBtn.Enabled = true;
}
}
}
private void lianjieBtn_Click(object sender, EventArgs e)
{
if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "" && textBox1.Text != "")
{
try
{
LJ.Constr = "server=" + comboBox1.Text + ";" + "database=training;" + "user=" + comboBox3.Text + ";" + "pwd=" + textBox1.Text + ";";
//string conStr = @"server=LAPTOP-ILT6KA7J\SQLEXPRESS;database=training;user=sa;pwd=sa";
string sql = "select * from Login";
SqlDataAdapter adapter = new SqlDataAdapter(sql, LJ.Constr);
LJ.Logindt.Clear();
adapter.Fill(LJ.Logindt);
MessageBox.Show("连接成功", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("内容不可为空", "提示");
}
}
private void quxiaoBtn_Click(object sender, EventArgs e)
{
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
textBox1.Text = "";
}
}
}
账号登陆
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp205
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void stuInit()
{
Class1.connect("Student");
Class1.connect("Class");
Class1.connect("StuCla");
Class1.connect("Ban");
}
private void teaInit()
{
Class1.connect("Student");
Class1.connect("Class");
Class1.connect("StuCla");
Class1.connect("Teacher");
Class1.connect("Ban");
}
private void leaInit()
{
Class1.connect("Student");
Class1.connect("Class");
Class1.connect("StuCla");
Class1.connect("Teacher");
Class1.connect("Ban");
}
private void Init()
{
Class1.connect("Student");
Class1.connect("Class");
Class1.connect("StuCla");
Class1.connect("Teacher");
Class1.connect("Leader");
Class1.connect("Ban");
}
private void dataLoad()
{
if (LJ.Login == 0)
{
stuInit();
}
else if (LJ.Login == 2)
{
teaInit();
}
else if (LJ.Login == 4)
{
leaInit();
}
else
{
Init();
}
}
private int login(int i)
{
foreach (DataRow row in LJ.Logindt.Rows)
{
if (row[i].ToString().Trim() == accountCom.Text)
{
if (row[i + 1].ToString().Trim() == passwordCom.Text)
{
LJ.Login = i;
LJ.ZhangHao = accountCom.Text;
return 1;
}
else
{
return 0;
}
}
}
return -1;
}
private void passwordCom_TextChanged(object sender, EventArgs e)
{
if (passwordCom.Text.Length < 8)
{
errorProvider2.SetError(passwordCom, "密码位数异常");
dengluBtn.Enabled = false;
}
else
{
errorProvider2.Clear();
if (passwordCom.Text.Length >= 8 && accountCom.Text.Length >= 8)
{
dengluBtn.Enabled = true;
}
}
}
private void accountCom_TextChanged(object sender, EventArgs e)
{
if (accountCom.Text.Length < 8)
{
errorProvider1.SetError(accountCom, "账号位数异常");
dengluBtn.Enabled = false;
}
else
{
errorProvider1.Clear();
if (passwordCom.Text.Length >= 8 && accountCom.Text.Length >= 8)
{
dengluBtn.Enabled = true;
}
}
}
private void dengluBtn_Click(object sender, EventArgs e)
{
List<DL> dL = new List<DL>();
dL.Add(new DL { Radio = radioButton1, T = 0, Form = new Form4() });
dL.Add(new DL { Radio = radioButton2, T = 2, Form = new Form5() });
dL.Add(new DL { Radio = radioButton3, T = 4, Form = new Form6() });
dL.Add(new DL { Radio = radioButton4, T = 6, Form = new Form7() });
if (passwordCom.Text != "" && accountCom.Text != "")
{
foreach (DL dl in dL)
{
if (dl.Radio.Checked)
{
int t = login(dl.T);
if (t == 1)
{
MessageBox.Show("登录成功", "提示");
this.Hide();
dl.Form.Show();
dataLoad();
}
else if (t == 0)
{
MessageBox.Show("密码错误", "提示");
}
else if (t == -1)
{
MessageBox.Show("账号错误", "提示");
}
}
}
}
else
{
MessageBox.Show("账号密码不可为空", "提示");
}
}
private void quxiaoBtn_Click(object sender, EventArgs e)
{
accountCom.Text = "";
passwordCom.Text = "";
radioButton1.Checked = true;
}
}
}
学生基本信息(部分)
//学生基本信息查看
private void kryptonRibbonGroupButton4_Click(object sender, EventArgs e)
{
panel2.BringToFront();
TextBox[] textBoxes = { textBox17, textBox18, textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27, textBox28, textBox29, textBox30 };
for (int i = 0; i < textBoxes.Length; i++)
{
textBoxes[i].Text = "";
}
studentDt.DataSource = LJ.Studentdt;
CurrencyManager myCM = (CurrencyManager)BindingContext[studentDt.DataSource];
myCM.SuspendBinding();//挂起数据绑定
studentDt.Columns[studentDt.Columns.Count - 1].Visible = false;
myCM.ResumeBinding();
rZhi("查看学生信息", "成功");
}
//学生详细信息查看
private void studentDt_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
index = e.RowIndex;
TextBox[] textBoxes = { textBox17, textBox18, textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27, textBox28, textBox29, textBox30 };
if (index > -1)
{
for (int i = 0; i < textBoxes.Length; i++)
{
textBoxes[i].Text = studentDt[i, index].Value.ToString().Trim();
}
LJ.ImageByte = (byte[])studentDt[studentDt.Columns.Count - 1, index].Value;
MemoryStream ms = new MemoryStream(LJ.ImageByte);//创建图片数据流
Bitmap bmap = new Bitmap(ms);//获取图片
ms.Close();
pictureBox1.Image = bmap;
}
}
代码太多了,就不全都写出来了,望各位谅解。预祝大家实训顺利!