学生信息管理系统完整版

一:先展示功能图:

主界面如下:

学生信息管理系统完整版_第1张图片

添加学生窗口:设置为父窗口和子窗口

学生信息管理系统完整版_第2张图片

添加成功图:


学生列表操作

学生信息管理系统完整版_第3张图片

删除学生:

学生信息管理系统完整版_第4张图片

添加学生:实现窗体间的通信

学生信息管理系统完整版_第5张图片

代码如下:

学生信息管理系统完整版_第6张图片

UI用户界面层代码:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace MySchoolUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

     
        private void 添加学生ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddStudent f = new AddStudent();
            f.MdiParent=this;
            f.Show();
        }

        private void 学生列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StudentListForm f = new StudentListForm();
            f.MdiParent = this;
            f.Show();

        }

       

       
}

AddStudent.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySchoolEntity;
using MySchoolBll;

namespace MySchoolUI
{
    public partial class AddStudent : Form
    {
        public AddStudent()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            student.Id = textId.Text;
            student.Name = textname.Text;
            student.Gender = radiomale.Checked ? "男" : "女";
            student.Major = textmajor.Text;
            student.Grade = int.Parse(textgrade.Text);
            student.TheClass =int.Parse( textclass.Text);
            StudentBll bll = new StudentBll();
            bll.insert(student);
           // MessageBox.Show("添加成功!");
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

        }
    }
}

StudentListForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySchoolBll;
using MySchoolEntity;

namespace MySchoolUI
{
    public partial class StudentListForm : Form
    {
        public StudentListForm()
        {
            InitializeComponent();
        }

        private void 查看_Click(object sender, EventArgs e)
        {            
            refreshDate();
        }

        private void refreshDate()
        {
            StudentBll bll = new StudentBll();
            List list = bll.getALl();
            gride.DataSource = list;
        }

        private void gride_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void StudentListForm_Load(object sender, EventArgs e)
        {
            gride.AutoGenerateColumns = false;
        }

        private void gride_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string content = gride.CurrentCell.Value.ToString();
            int row = gride.CurrentCell.RowIndex;
            int  col=gride.CurrentCell.ColumnIndex;
            string s = string.Format("你双击的单元格:{0}行{1}列 值为{2}", row + 1, col + 1, content);
            MessageBox.Show(s);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string id = gride.CurrentRow.Cells[0].Value.ToString();
            string name = gride.CurrentRow.Cells[1].Value.ToString();
            DialogResult r = MessageBox.Show("确实要删除["+id+" "+name+"]吗?", "提示", MessageBoxButtons.YesNo);
            if (r == DialogResult.Yes)
            {
                
                StudentBll bll = new StudentBll();
                bll.delete(id);
                refreshDate();
            }
            }

        private void button1_Click(object sender, EventArgs e)
        {
            AddStudent f = new AddStudent();
            f.ShowDialog();
            if (f.DialogResult == DialogResult.OK)
            {
                StudentBll bll = new StudentBll();
                refreshDate();
            }

        }
    }
}

bll层:业务逻辑层

studenBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySchoolEntity;
using MySchoolDAL;

namespace MySchoolBll
{
    public class StudentBll
    {
        public void insert(Student s)
        {  
           //检查业务逻辑
            if(s.Grade>2012||s.Grade<2009)
                throw new Exception("年纪错误!");
            StudentDal  dal=new StudentDal();
            dal.insert(s);
        
        }
        public void delete(string id)
        {
            StudentDal dal = new StudentDal();
            dal.delete(id);
        
        }

        public List getALl() {
            StudentDal dal = new StudentDal();
            return dal.getAll();
        
        }
    }
}

数据层访问层:DAl

DBhelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace MySchoolDAL
{
   public class DbHelper
   {
        static string connStr = "server=.;database=SampleDb;Integrated Security=true;";
       public static void myExecute(string sql)
       {
          
           SqlConnection conn = new SqlConnection();
           conn.ConnectionString = connStr;
           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = sql;
           cmd.Connection = conn;
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close(); 
       }

       public static SqlConnection createConnection()
       {
           return new SqlConnection(connStr); 
       
       }
    }
}

studentdal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MySchoolEntity;
namespace MySchoolDAL
{
    public class StudentDal
    {
        public void insert(Student student)
        {
            
          string sql = string.Format("insert into Student(Id,Name,Gender,Major,Grade,Class) values('{0}','{1}','{2}','{3}',{4},{5})",
                student.Id, student.Name, student.Gender, student.Major, student.Grade, student.TheClass);
          DbHelper.myExecute(sql);
        }
        public void delete(string id)
        {
            string sql = string.Format("delete from student where id='{0}'", id);
            DbHelper.myExecute(sql);
        
        }
        //查询所有学生
        public List getAll() {

            List list = new List();
            SqlConnection conn = DbHelper.createConnection();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from  student";
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read()) {
                Student student = new Student();
                student.Id = (string)reader["id"];
                student.Name = (string)reader["name"];
                student.Gender = (string)reader["gender"];
                student.Major = (string)reader["major"];
                student.Grade = (int)reader["grade"];
                student.TheClass = (int)reader["class"];
                list.Add(student);
            }
            reader.Close();
            conn.Close();
            return list;
        }

    }
}

实体类enity

student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySchoolEntity
{
    public  class Student
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Major { get; set; }
        public int Grade { get; set; }
        public int TheClass { get; set; }
    }
}

测试类test

using MySchoolDAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySchoolEntity;

namespace DalTest
{
    
    
   
    [TestClass()]
    public class StudentDalTest
    {

        /// 
        ///insert 的测试
        ///
        [TestMethod()]
        public void insertTest()
        {
            StudentDal target = new StudentDal(); 
            Student student =new Student();
            student.Id ="Bzu01";
            student.Name="测试生成";
            student.Major="CS";
            student.Gender="M";
            student.Grade=2011;
            student.TheClass=2;
            target.insert(student);
            
        }
    }
}




你可能感兴趣的:(.net,c#)