【小项目】学生管理系统_V1.1

原创 萧瑟  2016-05-27 11:55:21  阅读 1329 次 评论 1 条

前言

昨天大白老师说我们下午考试。如果上午考的是KTV系统,那么我们下午考的就是学生管理系统,刚好昨天钻研了一下,自己写了一节课,然后完善了一些,暂未发现有BUG,现在分享给爱学习的你。如果在学习的过程中遇到不懂的问题,欢迎找我一起讨论。我的企鹅号是381318751.

效果图

这个系统我会在今天下午进行一次更新的,是大版本的更新呦

我直接贴源码;程序晚上上传

程序的源码在最下方 V1.1版本更新: 1.更新学生模糊查询 2.更新班级信息查询 3.更新BUG

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 Student
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadstudent();
            comboBox1.Items.Add("男");
            comboBox1.Items.Add("女");

        }
        string str;
        SqlConnection conn;
        DataSet set;
        //加载学生信息
        public void loadstudent()
        {

            textBox2.Enabled = false;
            comboBox1.DropDownStyle = ComboBoxStyle.Simple;
            comboBox1.Enabled = false;
            textBox4.Enabled = false;
            textBox5.Enabled = false;
            textBox6.Enabled = false;
            str = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
            conn = new SqlConnection(str);
            try
            {
                conn.Open();
                string sql = "select * from dbo.UserInfo";

                SqlDataAdapter a = new SqlDataAdapter(sql,conn);
                set = new DataSet();
                a.Fill(set, "goods");
                conn.Close();
                loadshuju(set, "goods");
            }
            catch (Exception)
            {

                throw;
            }
        }
        //学生信息循环加载在listview
        public void loadshuju(DataSet a,string name)
        {
            listView1.Items.Clear();
            foreach (DataRow item in a.Tables[name].Rows)
            {
                ListViewItem b = new ListViewItem(item[0].ToString());
                b.SubItems.Add(item[1].ToString());
                b.SubItems.Add(item[2].ToString());
                b.SubItems.Add(item[3].ToString());
                b.SubItems.Add(item[4].ToString());
                b.SubItems.Add(item[5].ToString());
                listView1.Items.Add(b);
            }
        }
        //新增数据 清理数据 和禁止输入
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Enabled = true;
            textBox2.Clear();
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Enabled = true;
            textBox4.Clear();
            textBox4.Enabled = true;
            textBox6.Enabled = true;
            textBox5.Enabled = true;
            textBox5.Clear();
            textBox6.Clear();
        }
        //保存学生数据
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
                string sql = string.Format("insert UserInfo (Name,Sex,Class,Address,other) values ('{0}','{1}','{2}','{3}','{4}')", textBox2.Text, comboBox1.Text, textBox4.Text, textBox5.Text, textBox6.Text);
                SqlCommand comm = new SqlCommand(sql,conn);
                int result = comm.ExecuteNonQuery();
                if (result>0)
                {
                    MessageBox.Show("添加成功");
                    loadstudent();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
                conn.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //点击listview加载数据,显示在右面
        private void listView1_Click_1(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem a = listView1.SelectedItems[0];
                textBox1.Text = a.SubItems[0].Text;
                textBox2.Enabled = false;
                textBox2.Text = a.SubItems[1].Text;
                comboBox1.DropDownStyle = ComboBoxStyle.Simple;
                comboBox1.Enabled = false;
                comboBox1.Text = a.SubItems[2].Text;
                textBox4.Enabled = false;
                textBox4.Text = a.SubItems[3].Text;
                textBox5.Enabled = false;
                textBox5.Text = a.SubItems[4].Text;
                textBox6.Enabled = false;
                textBox6.Text = a.SubItems[5].Text;
            }
        }
        //右键删除数据
        private void delete_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0 && listView1.SelectedItems.Count<=1)
            {
                try
                {
                    ListViewItem a = listView1.SelectedItems[0];
                    int id = int.Parse(a.SubItems[0].Text);
                    conn.Open();
                    string sql = string.Format("delete from UserInfo where ID='{0}'",id);
                    SqlCommand comm = new SqlCommand(sql, conn);
                    int result = comm.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("删除成功");
                        loadstudent();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                    conn.Close();
                }
                catch (Exception)
                {

                    throw;
                }
            }
            else
            {
                MessageBox.Show("请选择一项删除");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

            textBox2.Enabled = true;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Enabled = true;
            textBox4.Enabled = true;
            textBox6.Enabled = true;
            textBox5.Enabled = true;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
                string sql = string.Format("update UserInfo set Name='{0}',Sex='{1}',Class='{2}',Address='{3}',other='{4}'where ID={5}", textBox2.Text, comboBox1.Text, textBox4.Text, textBox5.Text, textBox6.Text,textBox1.Text);
                SqlCommand comm = new SqlCommand(sql, conn);
                int result = comm.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("修改成功");
                    loadstudent();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
                conn.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //刷新
        private void button5_Click(object sender, EventArgs e)
        {
            loadstudent();
        }
        //班级查询com
        public void comjz()
        {
            try
            {
                conn.Open();
                string sql = "select distinct Class from UserInfo  ";
                SqlDataAdapter a = new SqlDataAdapter(sql, conn);
                DataSet comset = new DataSet();
                a.Fill(comset, "Class");
                conn.Close();
                comboBox2.DataSource = comset.Tables["Class"];
                comboBox2.DisplayMember = "Class";
            }
            catch (Exception)
            {

                throw;
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            comjz();

        }
        //班级查询
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string className = this.comboBox2.Text;
            try
            {
                conn.Open();
                string sql = string.Format("select *  from UserInfo where Class ='{0}'", className);
                SqlDataAdapter a = new SqlDataAdapter(sql, conn);
                DataSet comset = new DataSet();
                a.Fill(comset, "Class");
                conn.Close();
                loadshuju(comset, "Class");
                //comboBox2.DataSource = comset.Tables["Class"];
                //comboBox2.DisplayMember = "Class";
            }
            catch (Exception)
            {

                throw;
            }
        }
        //学生模糊查询
        private void button7_Click(object sender, EventArgs e)
        {
            string a = textBox3.Text;
            try
            {
                conn.Open();
                string sql = string.Format("select *  from UserInfo where Name like '%{0}%'", a);
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataSet set1 = new DataSet();
                adapter.Fill(set1, "areaSinger");
                conn.Close();
                loadshuju(set1, "areaSinger");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

就写了这么多 具体的东西都是套路,你懂得

下载地址

点我下载

本文地址:https://www.xsyrz.cn/WindowsForms-002.html
版权声明:本文为原创文章,版权归 萧瑟 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

评论列表

  1. ߹.
    ߹.  @回复

    效果图挂了哈[汗][汗][汗]