**
临近放假数据库实习,老师让做一个学生管理系统,时间匆忙就给了一天时间。所以就单单做了查询,添加,修改,删除四个功能,设计还是很low,勉勉强强应付了事,今天和大家分享一下。
思路
笔者想做一个可视化的窗体来管理学生信息系统,由于要连接数据库,我说使用的数据库为SQLSERVER。开始是一个登录页面,用于判别你是学生还是老师,之后就是信息管理页面,有四个功能。话不多说,直接上代码,由于代码过多,就不把代码全贴上去了。丢一个百度网盘,源码打包
链接:https://pan.baidu.com/s/13b62b7LGBD1_GlbfKDMfmw
提取码在下方留言即可。
login.cs:
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 学生管理系统_chen
{
public partial class login : Form
{
bool mov = false;
int xpos;
int ypos;
public login()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
string ss = "Server = localhost; database = 学生信息管理系统;Integrated Security = True";//integrated 各部分密切协调的;综合的;完整统一的
SqlConnection con = new SqlConnection(ss);//连接数据库
try
{
con.Open();
//messagebox.show("连接成功!");
}
catch
{
MessageBox.Show("数据库连接失败!");
}
string username = user.Text.Trim();//用户名
string pass = password.Text.Trim();//密码
string s1 = "";
if (radioButton1.Checked)
{
s1 = "select * from login_T where 教工号= " + username + "and 密码 = " + pass;//SQL语句,教师端登录
}
else if(radioButton2.Checked)
{
s1 = "select * from login_S where 学号= " + username + "and 密码 = " + pass;//SQL语句,学生端登录
}
SqlCommand cmd = new SqlCommand(s1, con)//执行语句
{
Connection = con,
CommandText = s1
};
try
{
object obj = cmd.ExecuteScalar();//查询数据库有无所输数据的函数
int cnt = (int)obj;
if (cnt != 0)
{
MessageBox.Show("登录成功");
if (radioButton2.Checked)
{
main m = new main();
m.Owner = this;
m.ShowDialog();
this.Close();
}
if(radioButton1.Checked)
{
teacher t = new teacher();
t.Owner = this;
t.ShowDialog();
this.Close();
}
}
con.Close();
}
catch //异常处理,obj未在数据库中,此事为null
{
MessageBox.Show("登陆失败");
}
}
private void button2_Click(object sender, EventArgs e)
{
user.Text = "";
password.Text = "";
}
private void login_MouseDown(object sender, MouseEventArgs e)
{
mov = true;
xpos = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
ypos = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
}
private void login_MouseMove(object sender, MouseEventArgs e)
{
if (mov)
{
this.Left += MousePosition.X - xpos;//根据鼠标x坐标确定窗体的左边坐标x
this.Top += MousePosition.Y - ypos;//根据鼠标的y坐标窗体的顶部,即Y坐标
xpos = MousePosition.X;
ypos = MousePosition.Y;
}
}
private void login_MouseUp(object sender, MouseEventArgs e)
{
mov = false;//停止移动
}
private void login_MouseLeave(object sender, EventArgs e)
{
xpos = 0; //设置初始状态
ypos = 0;
mov = false;
}
}
}
namespace 学生管理系统_chen
{
public partial class main : Form
{
SqlDataAdapter adp,adp1;
SqlCommand cmd;
SqlConnection con;
DataSet dat;
DataTable dt;
public main()
{
InitializeComponent();
string ss = "Server = localhost;database = 学生信息管理系统;Integrated Security = True";
//建立数据库连接
con = new SqlConnection(ss);
try
{
//开启连接
con.Open();
// MessageBox.Show("数据库连接成功!");
}
catch (Exception)
{
MessageBox.Show("数据库连接失败!");
}
}
private void main_Load(object sender, EventArgs e)
{
Showdata();
Showgrade();
}
private void Showgrade()
{
string s = "select C.cname as 课程,grade as 成绩 from SC join C on C.cno = SC.cno where sno = " + dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString();
adp = new SqlDataAdapter(s, con);
DataSet dat1 = new DataSet();
adp.Fill(dat1);
dataGridView2.DataSource = dat1.Tables[0];
}
private void Showdata()
{
string s = "select * from S";
adp = new SqlDataAdapter(s, con);
/*DataSet是数据集,DataTable是数据表,DataSet存储多个DataTable。DataSet和DataTable像是专门存储数据的一个容器,在你查询数据库得到一些结果时可以存在里面。*/
//dt = new DataTable();
//adp.Fill(dt);
//dataGridView1.DataSource = dt;
dat = new DataSet();
adp.Fill(dat);
dataGridView1.DataSource = dat.Tables[0];
}
private void showtable()//在textbox中显示第一行的信息
{
textBox1.Text = dat.Tables[0].Rows[0][0].ToString();
textBox2.Text = dat.Tables[0].Rows[0][1].ToString();
textBox3.Text = dat.Tables[0].Rows[0][2].ToString();
textBox4.Text = dat.Tables[0].Rows[0][3].ToString();
textBox5.Text = dat.Tables[0].Rows[0][4].ToString();
textBox6.Text = dat.Tables[0].Rows[0][5].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//textbox显示详细信息
{
textBox1.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString();//dataGirdView.currentrow表示选中行数
textBox2.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][1].ToString();
textBox3.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][2].ToString();
textBox4.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][3].ToString();
textBox5.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][4].ToString();
textBox6.Text = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][5].ToString();
Showgrade();
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("确认删除?");
string sno = dat.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString();
string sql = "delete from S where 学号=" + sno;//以学号作为删除的指标
cmd = new SqlCommand(sql, con);
int cnt = cmd.ExecuteNonQuery();
if (cnt > 0)
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
Showdata();
}
private void button4_Click(object sender, EventArgs e)
{
//int flag = 1;
string sno = textBox1.Text.Trim();
string name = textBox2.Text.Trim();
string sex = textBox3.Text.Trim();
string school = textBox4.Text.Trim();
int age;
Int32.TryParse(textBox5.Text.Trim(), out age);
string special = textBox6.Text.Trim();
string college = textBox7.Text.Trim();
string year = textBox8.Text.Trim();
string phone = textBox9.Text.Trim();
try //学号不能为空
{
string ss = "select * from S where 学号=" + sno;
SqlCommand cmd1 = new SqlCommand(ss, con);
object obj = cmd1.ExecuteScalar();
if (obj == null)//判断学号是否已存在
{
string sql = String.Format("insert into S values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", sno, name, sex, school, age, special,college,year,phone);
cmd = new SqlCommand(sql, con);
int cnt = cmd.ExecuteNonQuery();
if (cnt > 0)
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
Showdata();
}
else
{
MessageBox.Show("该学号已存在!!!");
}
}
catch
{
MessageBox.Show("学号不能为空!");
}
//if (String.IsNullOrEmpty(sno))
//{
// MessageBox.Show("学号不能为空!");
// flag = 0;
//}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("确认修改?");
string sno = textBox1.Text.Trim();
string name = textBox2.Text.Trim();
string sex = textBox3.Text.Trim();
string school = textBox4.Text.Trim();
int age;
Int32.TryParse(textBox5.Text.Trim(), out age);
string special = textBox6.Text.Trim();
if (String.IsNullOrEmpty(sno))
{
MessageBox.Show("学号不能为空!");
}
if (String.IsNullOrEmpty(name))
{
MessageBox.Show("姓名不能为空!");
}
string sql = String.Format("update S set 学号 = '{0}',姓名 = '{1}',性别 = '{2}',班级 = '{3}',年龄 = '{4}',专业 = '{5}' where 学号 = '{6}'", sno, name, sex, school, age, special,sno );
cmd = new SqlCommand(sql, con);
int cnt = cmd.ExecuteNonQuery();
if (cnt > 0)
{
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}
Showdata();
}
private void button5_Click(object sender, EventArgs e)
{
con.Close();
this.Close();
}
//('{0}','{1}','{2}','{3}','{4}','{5}')
private void button1_Click_1(object sender, EventArgs e)
{
string name = textBox7.Text.Trim();
string sno = textBox7.Text.Trim();
string sql = "select * from S where 1=1";
if (radioButton1.Checked)
{
sql += "and 姓名 like '%" + name +"%'";//模糊查询
}
if(radioButton2.Checked)
{ sql += "and 学号=" + sno; }
adp = new SqlDataAdapter(sql, con);
dat = new DataSet();
adp.Fill(dat);
dataGridView1.DataSource = dat.Tables[0];
showtable();
}
}
}