赞
踩
一个超级简单的学生选课系统,使用Windows窗体设计界面,使用C#语言实现各种功能,数据库使用的是SQL。由于时间原因,做的非常仓促,系统中没有查询功能,能够实现数据的增删改。下面开始系统演示吧。

我使用Windows窗体设计成这个样子 看着还算不错,嘻嘻。
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { if(pictureBox1.Location.X<150) { pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y); }//图标移动 else { if(comboBox1.Text == "学生") { string sql = "select * from Stu where Name = '" + textBox1.Text + "'and Password ='" + textBox2.Text + "'"; Dao dao = new Dao(); IDataReader dr = dao.read(sql); dr.Read(); string Sid = dr["ID"].ToString(); Form3 form3 = new Form3(Sid); form3.Show();//显示这个窗体 this.Hide();//隐藏这个窗体 } else { if(comboBox1.Text == "老师") { string sql = "select * from Teacher where Name = '" + textBox1.Text + "'and Password ='" + textBox2.Text + "'"; Dao dao = new Dao(); IDataReader dr = dao.read(sql); dr.Read(); string Tid = dr["ID"].ToString(); Form7 form7 = new Form7(Tid); form7.Show(); this.Hide(); } else { if(comboBox1.Text == "管理员") { Form5 form5 = new Form5(); form5.Show(); this.Hide(); } } } timer1.Stop(); } } //判断是否登录 private string login() { if(textBox1.Text == ""||textBox2.Text == ""||comboBox1.Text == "") { MessageBox.Show("输入不完整,请检查","提示",MessageBoxButtons.OK, MessageBoxIcon.Warning); } if(comboBox1.Text == "学生") { string sql = "select * from Stu where Name = '" +textBox1.Text+ "'and Password ='" +textBox2.Text+ "'"; Dao dao = new Dao(); IDataReader dr = dao.read(sql); if(dr.Read()) { return "学生"; } else { MessageBox.Show("用户名或密码错误"); return "-1"; } } if(comboBox1.Text == "老师") { string sql = "select * from Teacher where Name = '" +textBox1.Text+ "'and Password ='" +textBox2.Text+ "'"; Dao dao = new Dao(); IDataReader dr = dao.read(sql); if (dr.Read()) { return "老师"; } else { MessageBox.Show("用户名或密码错误"); return "-1"; } } if(comboBox1.Text == "管理员") { if(textBox1.Text == "admin"&&textBox2.Text == "admin") { return "管理员"; } else { MessageBox.Show("用户名或密码错误"); return "-1"; } } return "-1"; } private void button2_Click(object sender, EventArgs e) { textBox1.Text = null; textBox2.Text = null; comboBox1.Text = null; } private void textBox1_TextChanged(object sender, EventArgs e) { } //登录事件 private void button1_Click_1(object sender, EventArgs e) { if (login() != "-1") { timer1.Start();//启动计时器控件,图片开始移动 textBox1.Visible = false; textBox2.Visible = false; comboBox1.Visible = false; label1.Visible = false; label2.Visible = false; label3.Visible = false; } else { MessageBox.Show("信息错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit();//强行结束整个程序 } }
上述为系统登录的代码,请结合一些注释理解。系统登录界面,有三个权限,分别为学生、老师及管理员,在代码中,都有对应的代码 ,请仔细看。

public partial class Form3 : Form { string SID; public Form3(string Sid) { SID = Sid; InitializeComponent(); toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//显示当前时间 toolStripStatusLabel1.Text = "欢迎学号为" + SID + "的同学登录选课系统"; timer1.Start(); Table(); } public void Table() { dataGridView1.Rows.Clear(); string sql = "select * from Course"; Dao dao = new Dao(); IDataReader dr = dao.read(sql); while (dr.Read()) { string a, b, c, d; a = dr["ID"].ToString(); b = dr["Name"].ToString(); c = dr["Credit"].ToString(); d = dr["Teacher"].ToString(); string[] str = { a, b, c, d}; dataGridView1.Rows.Add(str); } dr.Close();//关闭链接 } private void timer1_Tick(object sender, EventArgs e) { toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//显示当前时间 } private void Form3_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit();//强行结束整个程序 } private void 选课ToolStripMenuItem_Click(object sender, EventArgs e) { string Cid = dataGridView1.SelectedCells[0].Value.ToString();//获取选中得课程号 string sql1 = "Select * from CourseChoose where Sid = '"+SID+"' and Cid = '"+Cid+"'"; Dao dao = new Dao(); IDataReader dc = dao.read(sql1); if(!dc.Read()) { string sql = "Insert into CourseChoose values('" + SID + "','" + Cid + "')"; int i = dao.Execute(sql); if(i>0) { MessageBox.Show("选课成功"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。