本文实例为大家分享了C#实现简单学生信息管理系统的具体代码,供大家参考,具体内容如下
一、运行环境windows,使用vs编译软件
二、主要功能
登录、添加学生信息、修改学生信息、删除学生信息、查询学生信息
三、实现步骤
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}");
}
}
效果如下图:
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, EventAr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