1.使用到的软件Microsoft Visual Studio(用来设计和编写WindowsForms程序)、Microsoft SQL Server(用来储存员工的信息并且与Microsoft Visual Studio进行连接进行增删查改的操作)。

2.项目由三层架构的设计方式来编写代码UI(表现层)、BLL:(业务逻辑层)、DAL:(数据访问层)、(Model)作为数据传递的载体。

实现效果图

第一步

下载Microsoft SQL Server连接到数据库

如果连接失败看看电脑里面的服务选项,看看这个三个服务是否在运行,没有的话手动打开即可。

打开后默认选项就行,然后进行连接

注:服务器名称一定要记住要不然Microsoft Visual Studio会连接不上服务器

连接成功后

在数据库新建数据库,名称为PersonDataBase,创建成功后打开数据库在表中分别建立三个表。

Admin Department Person

Admin表结构为

Department表结构为

Person表结构为

以上弄好之后第一步就完成了。

第二步下载Microsoft Visual Studio并打开并新建立C#窗体程序

一共有4个层我们先从UL层开始,创建窗体程序之后命名为UL

第一个界面也就是登录界面LoginForm

设计界面为

打开属性窗口将用户名的文本框将文本框控件名称设计为TB_UserName

同理密码文本框设计为

同理登录和退出的(Name)设置为Bth_Login 、Bth_Exit

点击空白区域看下名字是否为LoginForm

然后右键查看代码

LoginForm的代码为

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 UI
{public partial class LoginForm : Form{BLL.AdminManager am = new BLL.AdminManager();public LoginForm(){InitializeComponent();}private void Bth_Login_Click(object sender, EventArgs e){//获得用户名和密码string userName = TB_UserName.Text.Trim();string password = TB_Password.Text.Trim();//进行判断if (userName == ""){MessageBox.Show("账号不能为空");return;}if (password == ""){MessageBox.Show("密码不能为空!");return;}MODEL.Admin admin = am.GetAdmin(userName, password);if (admin != null){//MessageBox.Show("欢迎管理员"+admin.UserName);MainForm mainForm = new MainForm(admin);mainForm.Show();this.Visible = false;}else{MessageBox.Show("登陆失败!");}}private void Bth_Exit_Click(object sender, EventArgs e){this.Close();}//MODEL.Admin admin = am.GetAdmin(userName, password);private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){MainXForm mainxForm = new MainXForm();mainxForm.ShowDialog();}}
}

第二步在UL创建一个新的窗体

名称为MainXForm.cs

设计为

同理(1)姓名、(2)用户名、(3)密码、(4)确认密码、(5)邮箱、(6)超级管理员的复选框、(7)确认注册、(8)退出的控件名称分别设置为

(1)TBox_PersonName

(2)TBox_UserName

(3)TBox_Password1

(4)TBox_Password2

(5)TBox_Email

(6)CBox_IsSuperAdmin

(7)Btn_Res

(8)button2

MainXForm.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 System.Data.SqlClient;namespace UI
{   public partial class MainXForm : Form{BLL.AdminManager am = new BLL.AdminManager();public MainXForm(){InitializeComponent();}private void MainXForm_Load(object sender, EventArgs e){}private void Btn_Res_Click(object sender, EventArgs e){//1.先获得输入的注册信息string personName = TBox_PersonName.Text.Trim();string userName = TBox_UserName.Text.Trim();string password1 = TBox_Password1.Text.Trim();string password2 = TBox_Password2.Text.Trim();string email = TBox_Email.Text.Trim();bool isSuperAdmin=CBox_IsSuperAdmin.Checked;//2.判断输入的信息是否齐全if (personName == "" || userName == ""|| password1 == "" || password2 == ""){MessageBox.Show("信息没补全!", "提示");return;}//3.1,判断用户名是否已经存在if (am.ExistUserName(userName)){MessageBox.Show("用户名已经存在,无法注册", "提示");return;}//3.2,判断两个密码是否一样if (password1 != password2){MessageBox.Show("两次密码要一致!", "提示");return;}//4.将输入信息存到管理员对象MODEL.Admin admin = new MODEL.Admin();admin.PersonName = personName;admin.UserName = userName;admin.Password = password1;if (email == "")admin.Email = "未录入";elseadmin.Email = email;if (isSuperAdmin == true)admin.IsSuperAdmin = "是";elseadmin.IsSuperAdmin = "否";if (am.Register(admin)){MessageBox.Show("注册成功,将返回登陆窗口", "提示");this.Close();}else {MessageBox.Show("注测失败", "提示");}      }private void button2_Click(object sender, EventArgs e){System.Environment.Exit(0);}}
}

新建窗体为MainForm.cs

拖入一个menuStrip1的控件

设计为

MainForm.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 UI
{public partial class MainForm : Form{public MainForm(){InitializeComponent();}public MainForm(MODEL.Admin admin){InitializeComponent();this.Text = "欢迎您,"+admin.PersonName;}private void MainForm_Load(object sender, EventArgs e){//System.Environment.Exit(0);}//关闭事件private void MainForm_FormClosing(object sender, FormClosingEventArgs e){System.Environment.Exit(0);}private void 员工管理ToolStripMenuItem_Click(object sender, EventArgs e){//员工管理窗体ManagerForm mf = new ManagerForm();mf.MdiParent = this;mf.Show();}}
}

第五步

创建新的窗体名称为ManagerForm.cs

设计为

拖入一个叫DataGView的控件

还有一个叫CBBOX的控件

在CBBOX中设置文本为

并且在CBBOX的事件(属性上的小闪电标志)设置为

然后右键编辑DataGView

添加以下属性

员工ID

员工姓名

性别

年龄

住址

电话

部门

身份证

ManagerForm.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 UI
{public partial class ManagerForm : Form{BLL.PersonManager pm = new BLL.PersonManager();public ManagerForm(){InitializeComponent();}private void ManagerForm_Load(object sender, EventArgs e){//给员工插入数据DataGView_Person.DataSource = pm.GetAllPerson();CBBox_IsDel.SelectedIndex = 0;}private void CBBox_IsDel_SelectedIndexChanged(object sender, EventArgs e){if (CBBox_IsDel.SelectedIndex == 0){DataGView_Person.DataSource = pm.GetAllPerson();}else{DataGView_Person.DataSource = pm.GetAllPerson(true);}}}
}

目前UL层已经全部设计完毕了。

编辑BLL:(业务逻辑层)

在解决方案中,右键新建项目

新建一个类库名称为BLL

在新建的类库中添加两个类AdminManager.cs、PersonManager.cs

AdminManager.cs的代码为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace BLL
{public class AdminManager{DAL.AdminService ads = new DAL.AdminService();//登陆的方法public bool Login(string userName, string password) {return ads.Login(userName,password) > 0;}//获得管理员对象的方法public MODEL.Admin GetAdmin(string userName, string password){return ads.GetAdmin(userName, password);}//注册的方法public bool Register(MODEL.Admin admin) {return ads.Register(admin) > 0;}//用户名判存的方法public bool ExistUserName(String userName){return ads.ExistUserName(userName) > 0;}}
}

PersonManager.cs的代码为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace BLL
{public class PersonManager{DAL.PersonService ps = new DAL.PersonService();//获取所有员工的方法public List<MODEL.Person> GetAllPerson(bool isDel = false) {return ps.GetAllPerson(isDel);}}
}

目前BLL层已经全部设计完毕了。

建立DAL的类库同理第二步

AdminService中的代码为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;//数据层
namespace DAL
{public class AdminService{//登陆的方法public int Login(string userName, string password){//SQL语句//count函数是获取结果的行数string sql = "Select count(*) From Admin Where UserName=@username and Passsword=@password";//SQL参数SqlParameter[] ps = new SqlParameter[]{new SqlParameter("username",userName),new SqlParameter("password",password)};//执行SQL语句return SqlHelper.ExecuteScalaer(sql, ps);}/* public void GerAdmin(string userName, string password){//SQL语句string sql = "Select conn(*) From Admin Where UserName=@username and Passsword=@password";//SQL参数SqlParameter[] ps = new SqlParameter[]{new SqlParameter("username",userName),new SqlParameter("password",password)};//调用SQLHeloer中获取数据库中详细信息的方法DataTable dt = SqlHelper.ExecuteTable(sql, ps);}*///获取管理员的详细信息public MODEL.Admin GetAdmin(string userName, string password){//SQL语句string sql = "Select * From Admin Where UserName=@username and Password=@password";//SQL参数SqlParameter[] ps = new SqlParameter[]{new SqlParameter("username",userName),new SqlParameter("password",password)};//调用SQLHeloer中获取数据库中详细信息的方法DataTable dt = SqlHelper.ExecuteTable(sql, ps);//if (dt.Rows.Count == 0)return null;MODEL.Admin admin = RowDataToAdminObj(dt.Rows[0]);//return admin;}//将数据库的详细信息转换成类的对象的数据MODEL.Admin RowDataToAdminObj(DataRow row){//先创建一个管理员对象MODEL.Admin admin = new MODEL.Admin();//然后再将数据库的一行中的每个数据一个一个的进行转换admin.PersonID = row["PersonID"].ToString().Trim();admin.PersonName = row["PersonName"].ToString().Trim();admin.UserName = row["UserName"].ToString().Trim();admin.Password = row["Password"].ToString().Trim();admin.Email = row["Email"].ToString().Trim();admin.IsSuperAdmin = row["IsSuperAdmin"].ToString().Trim();//返回管理员对象return admin;}//注册public int Register(MODEL.Admin admin) { string sql="Insert Into Admin Values(@personName,@userName,@password,@email,@isSuperAdmin)";SqlParameter[] ps=new SqlParameter[]{new SqlParameter("personName",admin.PersonName),new SqlParameter("userName",admin.UserName),new SqlParameter("password",admin.Password),new SqlParameter("email",admin.Email),new SqlParameter("isSuperAdmin",admin.IsSuperAdmin)};return SqlHelper.ExecuteNonQuery(sql,ps);}//用户名判存的方法public int ExistUserName(string userName){string sql = "Select count(*) From Admin Where UserName=@username";SqlParameter[] ps=new SqlParameter[]{new SqlParameter("username",userName)};return SqlHelper.ExecuteScalaer(sql,ps);}}}

PersonService.cs中的代码为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace DAL
{public class PersonService{//获得所有在职的员工信息public List<MODEL.Person> GetAllPerson(bool isDel = false) {//编写查询所有在职员工信息的sql命令string sql = "Select * From Person Where IsDel = @isDel";//SQL参数SqlParameter[] ps=new SqlParameter[]{new SqlParameter("isDel",isDel)};//获取带有所以员工信息的数据表DataTable dt = SqlHelper.ExecuteTable(sql,ps);//判断数据表是否有数据if (dt.Rows.Count == 0)return null;//遍历数据表的每一行,将每一行的员工数据转换成员工对象List<MODEL.Person> personList = new List<MODEL.Person>();for (int i = 0; i < dt.Rows.Count;i++){MODEL.Person person = RowDataToPersonObj(dt.Rows[i]);personList.Add(person);}//返回员工列表return personList;}//将行数据转换成员工对象的方法MODEL.Person RowDataToPersonObj(DataRow row){//先创建一个员工对象MODEL.Person person = new MODEL.Person();//然后再将数据库的一行中的每个数据一个一个的进行转换person.PersonID = row["PersonID"].ToString().Trim();person.PersonName = row["PersonName"].ToString().Trim();person.Gender = row["Gender"].ToString().Trim();person.Age = int.Parse(row["Age"].ToString().Trim());person.Adress = row["Adress"].ToString().Trim();person.PhoneNum = row["PhoneNum"].ToString().Trim();person.DepartmentName = row["DepartmentName"].ToString().Trim();person.IsDel = (bool)row["IsDel"];person.IdNum = row["IdNum"].ToString().Trim();//返回员工对象return person;}}
}

SqlHelper.cs中的代码为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace DAL
{//数据库帮助类public class SqlHelper{//数据库链接字符串static readonly string connStr = "Data Source=legion拯救者;Initial Catalog=PersonDataBase;Integrated Security=True"; //这里输入的是你链接数据库链接的名字public static int ExecuteScalaer(string sql, params SqlParameter[] ps){//新建一个数据库渠道using (SqlConnection conn = new SqlConnection(connStr)){//打开数据库渠道conn.Open();//实例化数据库命令对象SqlCommand command = new SqlCommand(sql, conn);//添加参数command.Parameters.AddRange(ps);//执行数据库语句return (int)command.ExecuteScalar();}}public static DataTable ExecuteTable(string sql, params SqlParameter[] ps){//SqlDataAdapter da = new SqlDataAdapter(sql, connStr);//da.SelectCommand.Parameters.AddRange(ps);//DataTable dt = new DataTable();//da.Fill(dt);return dt;}public static int ExecuteNonQuery(string sql, params SqlParameter[] ps){//新建一个数据库渠道using (SqlConnection conn = new SqlConnection(connStr)){//打开数据库渠道conn.Open();//实例化数据库命令对象SqlCommand command = new SqlCommand(sql, conn);//添加参数command.Parameters.AddRange(ps);//执行数据库语句return (int)command.ExecuteNonQuery();}}}
}

注意注意

其中

这串代码需要注意前面的legion拯救者要和你当初的数据库名字一样,就是最开始连接数据库的那一步

static readonly string connStr = "Data Source=legion拯救者;Initial Catalog=PersonDataBase;Integrated Security=True";

建立MODEL层的类库同理

Admin中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace MODEL
{public class Admin{//声明了管理员的字段(类的成员变量)//属性,封装字段的手段  快捷键 Ctrl+R Ctrl+Estring personID;public string PersonID{get { return personID; }set { personID = value; }}string personName;public string PersonName{get { return personName; }set { personName = value; }}string userName;public string UserName{get { return userName; }set { userName = value; }}string password;public string Password{get { return password; }set { password = value; }}string email;public string Email{get { return email; }set { email = value; }}string isSuperAdmin;public string IsSuperAdmin{get { return isSuperAdmin; }set { isSuperAdmin = value; }}}
}

Person中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace MODEL
{public class Person{string personID;public string PersonID{  get { return personID; }set { personID = value; }}string personName;public string PersonName {get { return personName; }set { personName = value; }}string gender;public string Gender{get { return gender; }set { gender = value; }}int age;public int Age{get { return age; }set { age = value; }}string adress;public string Adress{get { return adress; }set { adress = value; }}string phoneNum;public string PhoneNum{get { return phoneNum; }set { phoneNum = value; }}string departmentName;public string DepartmentName{get { return departmentName; }set { departmentName = value; }}bool isDel;public bool IsDel{get { return isDel; }set { isDel = value; }}string idNum;public string IdNum{get { return idNum; }set { idNum = value; }}  }
}

以上就全部设计完成了

最后要将这些数据层互相引用

右键BLL

同理DAL

MODEL

UL

最后一步将Microsoft  Visual Studio连接至数据库

名字一定要是一样的,然后选择我们最开始创建的PersonDataBase数据库就完成了。

最后我们就可以在Microsoft Visual Studio中看到我们的数据库了

【使用C#建立一个简单的员工管理系统】相关推荐

  1. 用c++编写一个简单的员工管理系统

    文章目录 题目要求 一.设计思路 二.具体程序 总结 题目要求 高校工资管理系统 现一高校有五类职工:教师,实验员,行政人员,教师兼职实验员,行政人员兼 职教师. 为了实现工资发放的自动功能,现要求编 ...

  2. Hibernate学习——建立一个简单的Hibernate项目

    最近老师让做个web小应用,大三的时候学习过一点J2EE的东西,也做过一些web相关的XXX管理系统,都是用servlet,jsp这些完成的,虽然勉强能够完成任务,但其中各种代码掺杂在一起,不好看而且 ...

  3. mysql员工管理系统_简单的员工管理系统(Mysql+jdbc+Servlet+JSP)

    java java8 java开发 简单的员工管理系统(Mysql+jdbc+Servlet+JSP) 员工管理系统 因为学业要求,需要完成一个过关检测,但是因为检测之前没有做好准备,且想到之前用my ...

  4. 一个简单的后台管理系统

    一个简单的后台管理系统(1.0) 使用须知 一.数据库 1.1关于数据库的设计(重点) 1.2具体表设计 1.2.1角色表 1.2.2用户表 二.前端 1.关于需要注意的地方 2.新增注册页面 三.后 ...

  5. 一个简单的实习生管理系统

    写了一个简单的实习生管理系统,用于自己复习java后端.由于是复习java为目的,故而这个系统没有用框架,control层基本手写servlet完成.基本的增删改查,级联删除功能均有实现.适合java ...

  6. 我用python写了一个简单的企业管理系统

    课设必看!用python实现一个简单的企业管理系统,实现对公司信息的修改,部门的管理,员工的管理! 1.设计思路 没有利用面向对象的编程方法,仅仅关注业务实现 获取键盘输入的信息确定用户进行的操作,代 ...

  7. bat 删除文件_利用电脑文本文档建立一个简单方便的删除文件的小程序

    删除文不需要的文件或者资料,是日常工作中必定会遇到了. 各种的杀毒软件或者防护软件都具备删除文件的功能,例如360.腾讯电脑管家.这些操作起来其实也不是太麻烦! 不过呢!今天来和大家分享一个更简单的方 ...

  8. tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数

    tensorflow学习笔记二--建立一个简单的神经网络 2016-09-23 16:04 2973人阅读 评论(2) 收藏 举报  分类: tensorflow(4)  目录(?)[+] 本笔记目的 ...

  9. python小项目实例流程-Python小项目:快速开发出一个简单的学生管理系统

    原标题:Python小项目:快速开发出一个简单的学生管理系统 本文根据实际项目中的一部分api 设计抽象出来,实例化成一个简单小例子,暂且叫作「学生管理系统」. 这个系统主要完成下面增删改查的功能: ...

最新文章

  1. 直接送显示器!屏幕大了,打起代码才带劲!
  2. 一段话系列-领域模型是什么?
  3. HTML与CSS:设计滚动条(仅仅适用于IE浏览器)
  4. linux查看本机所有预设的系统变量,如何设置与查看Linux系统中的环境变量?
  5. 所谓高情商就是会说话--总结
  6. 微信又更新了,“拍一拍”玩法升级...
  7. 谷歌发布机器学习云平台 开放语音识别API
  8. 反向传播的目的,及其为什么要从后向前计算梯度
  9. Verilog 初学笔记--顺序操作 和 并行操作的一点思考(参考黑金教程:Verilog HDL那些事 建模篇)...
  10. 安徽省计算机二级水平考试试卷,安徽省计算机二级考试理论试题(附答案)
  11. 音视频开发技术,让智能家居更智能!
  12. 日常部署之OA办公系统源码OA协同办公源码包含CRM客户管理系统+内部聊天工具+自适应手机(含php源码)
  13. Xcode Missing file的解决方案
  14. 表格边框不显示怎么办
  15. 小白的proxmox ve(pve)打造AIO(all in boom)折腾日记 (二)装机篇(爱国者m2装机不完全教程)
  16. 总说业务,到底业务常用的指标有哪些
  17. 每天都在用电脑、玩手机的你,现在的眼睛有多干?
  18. 使用python代码给手机发短信详解(twilio的使用)
  19. 宠物王国四java正版_宠物王国单机版正版
  20. ECSHOP商品页调用热销商品的教程

热门文章

  1. js两只手指控制div图片放大缩小功能(2)
  2. 系统性能优化的十大策略(强烈推荐,建议收藏)
  3. 数据可视化之美 -- 以Matlab、Python为工具
  4. 单模/多模光纤能和单模/多模光模块混用吗?
  5. 解决jmeter5.4.3在高分辨率下的显示问题
  6. 暖风机家用最好的牌子 适合家用大面积的暖风机哪种好
  7. 免费售后——公司办公电脑配置单2500到4500档(包含显示器)
  8. 埃尔米特插值法在MATLAB中的应用
  9. 基于Qt的智能车载系统嵌入式项目(正点原子IMX6ULL开发板)
  10. javascript检测各种浏览器型号和版本、检测是否支持flash并显示版本