1.登录

loginForm.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 MySql.Data.MySqlClient;
namespace StaffManage
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void loginBtn_Click(object sender, EventArgs e)
{
if(unameText.Text.Trim().Length == 0)
{
MessageBox.Show("用户名不能为空");
//光标回到输入框中
unameText.Focus();//获取焦点
return;
}
if(upwdText.Text.Trim().Length == 0)
{
MessageBox.Show("密码不能为空");
upwdText.Focus();
return;
}
//验证账户和密码是否正确
//SqlConnection sqlConnection = new SqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course;");
MySqlConnection con = null;
try
{
con = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
con.Open();
string sql = string.Format("select * from login where uname='{0}' and upwd={1}", unameText.Text.Trim(), upwdText.Text.Trim());
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
User.Uname = unameText.Text.Trim();//用户名
User.Upwd = upwdText.Text.Trim();//密码
//跳转
MainForm mainForm = new MainForm();
mainForm.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
}
private void resetBtn_Click(object sender, EventArgs e)
{
//重置
unameText.Text = "";
upwdText.Text = string.Empty;
}
}
}
LoginForm.Designer.cs

namespace StaffManage
{
partial class LoginForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginForm));
this.unameLabel = new System.Windows.Forms.Label();
this.upwdLabel = new System.Windows.Forms.Label();
this.unameText = new System.Windows.Forms.TextBox();
this.upwdText = new System.Windows.Forms.TextBox();
this.loginBtn = new System.Windows.Forms.Button();
this.resetBtn = new System.Windows.Forms.Button();
this.userPicture = new System.Windows.Forms.PictureBox();
this.copyrightLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.userPicture)).BeginInit();
this.SuspendLayout();
//
// unameLabel
//
this.unameLabel.AutoSize = true;
this.unameLabel.Location = new System.Drawing.Point(226, 67);
this.unameLabel.Name = "unameLabel";
this.unameLabel.Size = new System.Drawing.Size(60, 15);
this.unameLabel.TabIndex = 1;
this.unameLabel.Text = "用户名:";
//
// upwdLabel
//
this.upwdLabel.AutoSize = true;
this.upwdLabel.Location = new System.Drawing.Point(233, 109);
this.upwdLabel.Name = "upwdLabel";
this.upwdLabel.Size = new System.Drawing.Size(53, 15);
this.upwdLabel.TabIndex = 1;
this.upwdLabel.Text = "密 码:";
//
// unameText
//
this.unameText.Location = new System.Drawing.Point(292, 64);
this.unameText.Name = "unameText";
this.unameText.Size = new System.Drawing.Size(181, 25);
this.unameText.TabIndex = 0;
//
// upwdText
//
this.upwdText.Location = new System.Drawing.Point(292, 106);
this.upwdText.Name = "upwdText";
this.upwdText.PasswordChar = '*';
this.upwdText.Size = new System.Drawing.Size(181, 25);
this.upwdText.TabIndex = 1;
//
// loginBtn
//
this.loginBtn.Location = new System.Drawing.Point(292, 155);
this.loginBtn.Name = "loginBtn";
this.loginBtn.Size = new System.Drawing.Size(75, 23);
this.loginBtn.TabIndex = 2;
this.loginBtn.Text = "登录";
this.loginBtn.UseVisualStyleBackColor = true;
this.loginBtn.Click += new System.EventHandler(this.loginBtn_Click);
//
// resetBtn
//
this.resetBtn.Location = new System.Drawing.Point(398, 155);
this.resetBtn.Name = "resetBtn";
this.resetBtn.Size = new System.Drawing.Size(75, 23);
this.resetBtn.TabIndex = 3;
this.resetBtn.Text = "重置";
this.resetBtn.UseVisualStyleBackColor = true;
this.resetBtn.Click += new System.EventHandler(this.resetBtn_Click);
//
// userPicture
//
this.userPicture.Image = ((System.Drawing.Image)(resources.GetObject("userPicture.Image")));
this.userPicture.Location = new System.Drawing.Point(28, 31);
this.userPicture.Name = "userPicture";
this.userPicture.Size = new System.Drawing.Size(166, 188);
this.userPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.userPicture.TabIndex = 6;
this.userPicture.TabStop = false;
//
// copyrightLabel
//
this.copyrightLabel.AutoSize = true;
this.copyrightLabel.Location = new System.Drawing.Point(332, 213);
this.copyrightLabel.Name = "copyrightLabel";
this.copyrightLabel.Size = new System.Drawing.Size(179, 15);
this.copyrightLabel.TabIndex = 7;
this.copyrightLabel.Text = "Copyright © xx版权所有";
//
// LoginForm
//
this.AcceptButton = this.loginBtn;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(533, 247);
this.Controls.Add(this.copyrightLabel);
this.Controls.Add(this.userPicture);
this.Controls.Add(this.resetBtn);
this.Controls.Add(this.loginBtn);
this.Controls.Add(this.upwdText);
this.Controls.Add(this.unameText);
this.Controls.Add(this.upwdLabel);
this.Controls.Add(this.unameLabel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "LoginForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "员工管理系统--登录";
((System.ComponentModel.ISupportInitialize)(this.userPicture)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label unameLabel;
private System.Windows.Forms.Label upwdLabel;
private System.Windows.Forms.TextBox unameText;
private System.Windows.Forms.TextBox upwdText;
private System.Windows.Forms.Button loginBtn;
private System.Windows.Forms.Button resetBtn;
private System.Windows.Forms.PictureBox userPicture;
private System.Windows.Forms.Label copyrightLabel;
}
}
View Code
2.主页面MainForm

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.Threading.Tasks;
using System.Windows.Forms;
namespace StaffManage
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
PwdForm pwdForm = new PwdForm();
pwdForm.MdiParent = this;
pwdForm.Show();
}
private void 员工列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
//先判断当前窗口中有没有员工列表这个窗体
//如果有则将其放到所有窗口的最前面,如果没有就打开窗体
foreach(Form item in this.MdiChildren)
{
if(item.GetType().Name == "StaffForm")
{
item.BringToFront();
return;
}
}
//如果没有该窗体的情况:
StaffForm staffForm = new StaffForm();
staffForm.MdiParent = this;
staffForm.Show();
}
private void 添加员工ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddStaffForm addStaffForm = new AddStaffForm();
addStaffForm.MdiParent = this;
addStaffForm.Show();
}
private void 部门列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(Form item in this.MdiChildren)
{
if(item.GetType().Name == "DeptForm")
{
item.BringToFront();
return;
}
}
DeptForm deptForm = new DeptForm();
deptForm.MdiParent = this;
deptForm.Show();
}
private void 添加部门ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddDeptForm addDeptForm = new AddDeptForm();
addDeptForm.MdiParent = this;
addDeptForm.Show();
}
private void 关于我们ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form item in this.MdiChildren)
{
if( item.GetType().Name == "HelpForm")
{
item.BringToFront();
return;
}
}
HelpForm helpForm = new HelpForm();
helpForm.MdiParent = this;
helpForm.Show();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
//员工查询
StaffForm staffForm = new StaffForm();
staffForm.MdiParent = this;
staffForm.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
//部门查询
DeptForm deptForm = new DeptForm();
deptForm.MdiParent = this;
deptForm.Show();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialogResult = MessageBox.Show("确定退出系统吗? ", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dialogResult == DialogResult.Yes)
{
Application.ExitThread();
}
else
{
//取消事件
e.Cancel = true;
}
}
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
//退出系统:this.Close()会继承上面的窗体关闭操作
//提示框与MainForm_FormClosing里面的提示框内容相同
this.Close();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
//退出系统
this.Close();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
//关闭所有子窗体
DialogResult dialogResult = MessageBox.Show("确定要关闭所有子窗体吗? ","系统提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(dialogResult == DialogResult.Yes)
{
//激活状态的子窗体
Form activeChild = this.ActiveMdiChild;
while(activeChild != null)
{
//关闭已打开的子窗体
activeChild.Close();
activeChild = this.ActiveMdiChild;
}
}
}
private void MainForm_Load(object sender, EventArgs e)
{
toolStripStatusLabel2.Text = User.Uname;
}
private void timer1_Tick_1(object sender, EventArgs e)
{
DateTime dateTime = DateTime.Now;
toolStripStatusLabel3.Text = dateTime.ToString("yyyy年MM月dd日 HH:mm:ss");
}
}
}
MainForm.Designer.cs

namespace StaffManage
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.系统管理ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.修改密码ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.退出系统ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.员工管理ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.员工列表ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.添加员工ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.部门管理ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.部门列表ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.添加部门ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.关于我们ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel3 = new System.Windows.Forms.ToolStripStatusLabel();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.menuStrip1.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.系统管理ToolStripMenuItem,
this.员工管理ToolStripMenuItem,
this.部门管理ToolStripMenuItem,
this.帮助ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(800, 28);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// 系统管理ToolStripMenuItem
//
this.系统管理ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.修改密码ToolStripMenuItem,
this.toolStripSeparator1,
this.退出系统ToolStripMenuItem});
this.系统管理ToolStripMenuItem.Name = "系统管理ToolStripMenuItem";
this.系统管理ToolStripMenuItem.Size = new System.Drawing.Size(102, 24);
this.系统管理ToolStripMenuItem.Text = "系统管理(&S)";
//
// 修改密码ToolStripMenuItem
//
this.修改密码ToolStripMenuItem.Name = "修改密码ToolStripMenuItem";
this.修改密码ToolStripMenuItem.Size = new System.Drawing.Size(172, 26);
this.修改密码ToolStripMenuItem.Text = "修改密码(C)";
this.修改密码ToolStripMenuItem.Click += new System.EventHandler(this.修改密码ToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(169, 6);
//
// 退出系统ToolStripMenuItem
//
this.退出系统ToolStripMenuItem.Name = "退出系统ToolStripMenuItem";
this.退出系统ToolStripMenuItem.Size = new System.Drawing.Size(172, 26);
this.退出系统ToolStripMenuItem.Text = "退出系统(E)";
this.退出系统ToolStripMenuItem.Click += new System.EventHandler(this.退出系统ToolStripMenuItem_Click);
//
// 员工管理ToolStripMenuItem
//
this.员工管理ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.员工列表ToolStripMenuItem,
this.添加员工ToolStripMenuItem});
this.员工管理ToolStripMenuItem.Name = "员工管理ToolStripMenuItem";
this.员工管理ToolStripMenuItem.Size = new System.Drawing.Size(108, 24);
this.员工管理ToolStripMenuItem.Text = "员工管理(M)";
//
// 员工列表ToolStripMenuItem
//
this.员工列表ToolStripMenuItem.Name = "员工列表ToolStripMenuItem";
this.员工列表ToolStripMenuItem.Size = new System.Drawing.Size(173, 26);
this.员工列表ToolStripMenuItem.Text = "员工列表(L)";
this.员工列表ToolStripMenuItem.Click += new System.EventHandler(this.员工列表ToolStripMenuItem_Click);
//
// 添加员工ToolStripMenuItem
//
this.添加员工ToolStripMenuItem.Name = "添加员工ToolStripMenuItem";
this.添加员工ToolStripMenuItem.Size = new System.Drawing.Size(173, 26);
this.添加员工ToolStripMenuItem.Text = "添加员工(A)";
this.添加员工ToolStripMenuItem.Click += new System.EventHandler(this.添加员工ToolStripMenuItem_Click);
//
// 部门管理ToolStripMenuItem
//
this.部门管理ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.部门列表ToolStripMenuItem,
this.添加部门ToolStripMenuItem});
this.部门管理ToolStripMenuItem.Name = "部门管理ToolStripMenuItem";
this.部门管理ToolStripMenuItem.Size = new System.Drawing.Size(104, 24);
this.部门管理ToolStripMenuItem.Text = "部门管理(D)";
//
// 部门列表ToolStripMenuItem
//
this.部门列表ToolStripMenuItem.Name = "部门列表ToolStripMenuItem";
this.部门列表ToolStripMenuItem.Size = new System.Drawing.Size(173, 26);
this.部门列表ToolStripMenuItem.Text = "部门列表(L)";
this.部门列表ToolStripMenuItem.Click += new System.EventHandler(this.部门列表ToolStripMenuItem_Click);
//
// 添加部门ToolStripMenuItem
//
this.添加部门ToolStripMenuItem.Name = "添加部门ToolStripMenuItem";
this.添加部门ToolStripMenuItem.Size = new System.Drawing.Size(173, 26);
this.添加部门ToolStripMenuItem.Text = "添加部门(A)";
this.添加部门ToolStripMenuItem.Click += new System.EventHandler(this.添加部门ToolStripMenuItem_Click);
//
// 帮助ToolStripMenuItem
//
this.帮助ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.关于我们ToolStripMenuItem});
this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(75, 24);
this.帮助ToolStripMenuItem.Text = "帮助(H)";
//
// 关于我们ToolStripMenuItem
//
this.关于我们ToolStripMenuItem.Name = "关于我们ToolStripMenuItem";
this.关于我们ToolStripMenuItem.Size = new System.Drawing.Size(173, 26);
this.关于我们ToolStripMenuItem.Text = "关于我们(U)";
this.关于我们ToolStripMenuItem.Click += new System.EventHandler(this.关于我们ToolStripMenuItem_Click);
//
// toolStrip1
//
this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1,
this.toolStripButton2,
this.toolStripButton3,
this.toolStripButton4});
this.toolStrip1.Location = new System.Drawing.Point(0, 28);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(800, 27);
this.toolStrip1.TabIndex = 1;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(93, 24);
this.toolStripButton1.Text = "员工查询";
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
//
// toolStripButton2
//
this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton2.Name = "toolStripButton2";
this.toolStripButton2.Size = new System.Drawing.Size(93, 24);
this.toolStripButton2.Text = "部门查询";
this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);
//
// toolStripButton3
//
this.toolStripButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton3.Image")));
this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton3.Name = "toolStripButton3";
this.toolStripButton3.Size = new System.Drawing.Size(93, 24);
this.toolStripButton3.Text = "退出系统";
this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click);
//
// toolStripButton4
//
this.toolStripButton4.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton4.Image")));
this.toolStripButton4.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton4.Name = "toolStripButton4";
this.toolStripButton4.Size = new System.Drawing.Size(138, 24);
this.toolStripButton4.Text = "关闭所有子窗口";
this.toolStripButton4.Click += new System.EventHandler(this.toolStripButton4_Click);
//
// statusStrip1
//
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1,
this.toolStripStatusLabel2,
this.toolStripStatusLabel3});
this.statusStrip1.Location = new System.Drawing.Point(0, 424);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 26);
this.statusStrip1.TabIndex = 2;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(144, 20);
this.toolStripStatusLabel1.Text = "当前登录的用户是:";
//
// toolStripStatusLabel2
//
this.toolStripStatusLabel2.Name = "toolStripStatusLabel2";
this.toolStripStatusLabel2.Size = new System.Drawing.Size(163, 20);
this.toolStripStatusLabel2.Text = "toolStrpStatusLabel2";
//
// toolStripStatusLabel3
//
this.toolStripStatusLabel3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripStatusLabel3.Name = "toolStripStatusLabel3";
this.toolStripStatusLabel3.Size = new System.Drawing.Size(192, 20);
this.toolStripStatusLabel3.Text = "0000年00月00日 00:00:00";
this.toolStripStatusLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.toolStrip1);
this.Controls.Add(this.menuStrip1);
this.IsMdiContainer = true;
this.MainMenuStrip = this.menuStrip1;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "员工管理系统---主界面";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.Load += new System.EventHandler(this.MainForm_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 系统管理ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 员工管理ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 部门管理ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 修改密码ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem 退出系统ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 员工列表ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 添加员工ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 部门列表ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 添加部门ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 关于我们ToolStripMenuItem;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
private System.Windows.Forms.ToolStripButton toolStripButton3;
private System.Windows.Forms.ToolStripButton toolStripButton4;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3;
private System.Windows.Forms.Timer timer1;
}
}
View Code
3.修改密码PwdForm

PwdForm.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 MySql.Data.MySqlClient;
namespace StaffManage
{
public partial class PwdForm : Form
{
public PwdForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (startpwd.Text.Trim().Length == 0)
{
MessageBox.Show("请输入原密码!");
startpwd.Focus();
return;
}
//原密码是否错误
if (startpwd.Text.Trim() != User.Upwd)
{
MessageBox.Show("原密码不正确..");
startpwd.Focus();
return;
}
if (newpwd.Text.Trim().Length == 0)
{
MessageBox.Show("请输入新密码!");
newpwd.Focus();
return;
}
//新密码和旧密码不能一样
if(newpwd.Text.Trim() == startpwd.Text.Trim())
{
MessageBox.Show("新密码与旧密码相同!");
newpwd.Focus();
return;
}
if (acpwd.Text.Trim().Length == 0)
{
MessageBox.Show("请输入确认密码!");
acpwd.Focus();
return;
}
//新密码与确认密码是否一致
if (newpwd.Text.Trim() != acpwd.Text.Trim())
{
MessageBox.Show("新密码与确认密码不一致");
acpwd.Focus();
return;
}
//修改数据库
MySqlConnection connection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
connection.Open();
string sql = string.Format("update login set upwd='{0}' where uname='{1}'",
newpwd.Text.Trim(), User.Uname);
MySqlCommand command= new MySqlCommand(sql, connection);
int result = command.ExecuteNonQuery();
if (result > 0)
{
//更新用户类中的密码
User.Upwd= newpwd.Text.Trim();
startpwd.Text = "";
newpwd.Text = "";
acpwd.Text = "";
DialogResult dr = MessageBox.Show("修改成功,请重新登录..","系统提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
//重启
Application.Restart();
}
}
connection.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//关闭当前的窗口
this.Close();
}
}
}
PwdForm.Designer.cs

namespace StaffManage
{
partial class PwdForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.startpwd = new System.Windows.Forms.TextBox();
this.newpwd = new System.Windows.Forms.TextBox();
this.acpwd = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(94, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(67, 15);
this.label1.TabIndex = 0;
this.label1.Text = "原密码:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(94, 119);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(67, 15);
this.label2.TabIndex = 1;
this.label2.Text = "新密码:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(64, 171);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(97, 15);
this.label3.TabIndex = 2;
this.label3.Text = "确认新密码:";
//
// startpwd
//
this.startpwd.Location = new System.Drawing.Point(167, 60);
this.startpwd.Name = "startpwd";
this.startpwd.Size = new System.Drawing.Size(199, 25);
this.startpwd.TabIndex = 0;
//
// newpwd
//
this.newpwd.Location = new System.Drawing.Point(167, 116);
this.newpwd.Name = "newpwd";
this.newpwd.Size = new System.Drawing.Size(199, 25);
this.newpwd.TabIndex = 1;
//
// acpwd
//
this.acpwd.Location = new System.Drawing.Point(167, 168);
this.acpwd.Name = "acpwd";
this.acpwd.Size = new System.Drawing.Size(199, 25);
this.acpwd.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(167, 225);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "修改";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(291, 225);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 4;
this.button2.Text = "关闭";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// PwdForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(442, 298);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.acpwd);
this.Controls.Add(this.newpwd);
this.Controls.Add(this.startpwd);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "PwdForm";
this.Text = "修改密码";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox startpwd;
private System.Windows.Forms.TextBox newpwd;
private System.Windows.Forms.TextBox acpwd;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
View Code
4.员工管理窗口StaffForm

StaffForm.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;
using MySql.Data.MySqlClient;
namespace StaffManage
{
public partial class StaffForm : Form
{
public StaffForm()
{
InitializeComponent();
}
private void StaffForm_Load(object sender, EventArgs e)
{
//初始化DataDridView列表数据
setDataGridView();
//初始化ComboBox下拉框
setComboBox();
//
}
/// <summary>
/// 初始化DataGridView数据列表
/// </summary>
private void setDataGridView()
{
MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
//string sql = string.Format("select sId as '编号',sCard as '工号',sName as '姓名',\r\nsSex as '性别',sAge as '年龄',sAddress as '地址',\r\ndeptName as '部门',sWages as '工资'\r\nfrom staffinfo s INNER JOIN deptinfo d on s.deptId = d.deptId;");
//条件查询
string sql = string.Format("select sId as '编号',sCard as '工号',sName as '姓名',\r\nsSex as '性别',sAge as '年龄',sAddress as '地址',\r\ndeptName as '部门',sWages as '工资'\r\nfrom staffinfo s INNER JOIN deptinfo d on s.deptId = d.deptId where 1=1");
if (textBox1.Text.Trim().Length !=0)
{
//注意最前面要写空格
sql += string.Format(" and sCard like '%{0}%'", textBox1.Text.Trim());
}
if(textBox2.Text.Trim().Length !=0)
{
sql += string.Format(" and sName like '%{0}'", textBox2.Text.Trim());
}
if(comboBox1.SelectedIndex > 0)
{
sql += string.Format(" and deptName like '%{0}'", comboBox1.Text.Trim());
}
sql += string.Format(" ORDER BY 编号");
MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection);
DataSet ds = new DataSet();
msda.Fill(ds,"table1");
dataGridView1.DataSource = ds.Tables["table1"];
mySqlConnection.Close();
}
/// <summary>
/// 初始化ComboBox下拉框
/// </summary>
private void setComboBox()
{
MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("select deptId,deptName from deptinfo");
MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection);
DataSet ds = new DataSet();
//msda.Fill(ds);
msda.Fill(ds, "depts");
//添加请选择
DataRow dr = ds.Tables["depts"].NewRow();
dr[0] = "0";
dr[1] = "请选择";
ds.Tables["depts"].Rows.InsertAt(dr, 0);
//显示值
comboBox1.DisplayMember = "deptName";
//隐藏值
comboBox1.ValueMember = "deptId";
comboBox1.DataSource = ds.Tables["depts"];
mySqlConnection.Close();
}
private void button1_Click(object sender, EventArgs e)
{
setDataGridView();
}
//点击修改按钮
private void button2_Click(object sender, EventArgs e)
{
//点击修改按钮的时候,弹出修改页面(修改和添加页面是同一个)
//修改事件
AddStaffForm addStaffForm = new AddStaffForm();
//拿到dataGridView1的当前行的第一列的值,返回值是object类型
addStaffForm.flag = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
//打开窗体:指定父窗体
addStaffForm.MdiParent = this.MdiParent;
addStaffForm.Show();
addStaffForm.FormClosed += new FormClosedEventHandler(StaffForm_FormClosed);
}
private void StaffForm_FormClosed(object sender, FormClosedEventArgs e)
{
//关闭窗体的时候刷新
setDataGridView();
}
private void button3_Click(object sender, EventArgs e)
{
//删除操作
DialogResult dialogResult = MessageBox.Show("确定要删除吗【" + dataGridView1.CurrentRow.Cells[2].Value + "】吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dialogResult == DialogResult.Yes)
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("delete from staffInfo where sId={0}"
, Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int result = mySqlCommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("删除成功");
setDataGridView();//刷新
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
}
}
}
StaffForm.Designer.cs

namespace StaffManage
{
partial class StaffForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(12, 26);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(797, 105);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "搜索条件";
//
// button1
//
this.button1.Location = new System.Drawing.Point(712, 41);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(79, 32);
this.button1.TabIndex = 6;
this.button1.Text = "查询";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(560, 47);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(130, 23);
this.comboBox1.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(329, 47);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(131, 25);
this.textBox2.TabIndex = 4;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 47);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(131, 25);
this.textBox1.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(481, 50);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(82, 15);
this.label3.TabIndex = 2;
this.label3.Text = "员工部门:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(250, 50);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(82, 15);
this.label2.TabIndex = 1;
this.label2.Text = "员工姓名:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(18, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 15);
this.label1.TabIndex = 0;
this.label1.Text = "员工工号:";
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 137);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 27;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(797, 352);
this.dataGridView1.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(278, 495);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 39);
this.button2.TabIndex = 2;
this.button2.Text = "修改";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(464, 495);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(83, 39);
this.button3.TabIndex = 3;
this.button3.Text = "删除";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// StaffForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(830, 546);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.groupBox1);
this.Name = "StaffForm";
this.Text = "员工列表";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.StaffForm_FormClosed);
this.Load += new System.EventHandler(this.StaffForm_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
View Code
5.员工添加/修改窗口AddStaffForm

AddStaffForm.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StaffManage
{
public partial class AddStaffForm : Form
{
//添加一个变量
//用来区分当前的页面是添加还是修改员工信息
public int flag = 0;//0表示添加,大于0表示修改功能
public AddStaffForm()
{
InitializeComponent();
}
private void AddStaffForm_Load(object sender, EventArgs e)
{
//加载的时候判断添加还是修改
if(flag == 0)
{
//添加功能
button1.Text = "添加";
//1.获取下拉列表
setComboBox();
}
else
{
//修改功能
button1.Text = "修改";
//1.获取下拉列表
setComboBox();
//2.初始化窗体表数据
setStaffById();
}
}
/// <summary>
/// 初始化修改窗口的员工信息
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void setStaffById()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("select * from staffInfo where sId = {0}", flag);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
if (mySqlDataReader.Read())
{
sCardText.Text = mySqlDataReader["sCard"].ToString();
sNameText.Text = mySqlDataReader["sName"].ToString();
if (mySqlDataReader["sSex"].ToString() == "男")
{
nanBtn.Checked = true;
}
else
{
nvBtn.Checked = true;
}
ageCombo.Text = mySqlDataReader["sAge"].ToString();
sAddressText.Text = mySqlDataReader["sAddress"].ToString();
deptCombo.SelectedIndex = Convert.ToInt32(mySqlDataReader["deptId"]);
numericUpDown1.Value = Convert.ToDecimal(mySqlDataReader["sWages"]);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
/// <summary>
/// 获取下拉列表
/// </summary>
private void setComboBox()
{
//年龄下拉框
for(int i = 1; i < 121; i++)
{
ageCombo.Items.Add(i);
}
ageCombo.SelectedItem = 18;//设置默认值
nanBtn.Checked = true;//默认性别男
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("select deptId,deptName from deptinfo");
MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection);
DataSet ds = new DataSet();
//msda.Fill(ds);
msda.Fill(ds, "depts");
//添加请选择
DataRow dr = ds.Tables["depts"].NewRow();
dr[0] = "0";
dr[1] = "请选择";
ds.Tables["depts"].Rows.InsertAt(dr, 0);
//显示值
deptCombo.DisplayMember = "deptName";
//隐藏值
deptCombo.ValueMember = "deptId";
deptCombo.DataSource = ds.Tables["depts"];
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
//非空认证
if (CheckInput())
{
//点击添加的时候,判断是添加还是修改员工
if (flag == 0)
{
//添加功能
insertStaff();
}
else
{
//修改功能
updateStaff();
}
}
}
/// <summary>
/// 非空认证
/// </summary>
private Boolean CheckInput()
{
if(sCardText.Text.Trim().Length == 0)
{
MessageBox.Show("员工工号不能为空");
sCardText.Focus();
return false;
}
if(sNameText.Text.Trim().Length == 0)
{
MessageBox.Show("员工姓名不能为空");
sNameText.Focus();
return false;
}
if(sAddressText.Text.Trim().Length == 0)
{
MessageBox.Show("员工地址不能为空");
sAddressText.Focus();
return false;
}
if(deptCombo.Text.Trim() == "请选择")
{
MessageBox.Show("请选择部门");
deptCombo.Focus();
return false;
}
//年龄和性别有默认值
return true;
}
/// <summary>
/// 添加员工信息
/// </summary>
private void insertStaff()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("insert into staffInfo(`sCard`,`sName`,`sSex`,`sAge`,`sAddress`,`deptId`,`sWages`) values('{0}','{1}','{2}',{3},'{4}',{5},{6})"
, sCardText.Text.Trim()
, sNameText.Text.Trim()
, nanBtn.Checked == true ? "男" : "女"
, Convert.ToInt32(ageCombo.Text)
, sAddressText.Text.Trim()
, (int)deptCombo.SelectedValue//部门号是被隐藏了的
, numericUpDown1.Value
);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int result = mySqlCommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("添加成功");
this.Close();
}
else
{
MessageBox.Show("添加失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
/// <summary>
/// 修改员工信息
/// </summary>
private void updateStaff()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("update staffInfo set sCard='{0}',sName='{1}',sSex='{2}',sAge={3},sAddress='{4}',deptId={5},sWages={6} where sId={7}"
, sCardText.Text.Trim()
, sNameText.Text.Trim()
, nanBtn.Checked == true ? "男" : "女"
, Convert.ToInt32(ageCombo.Text)
, sAddressText.Text.Trim()
, (int)deptCombo.SelectedValue//部门号是被隐藏了的
, numericUpDown1.Value
, flag);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int result = mySqlCommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("修改成功");
this.Close();
}
else
{
MessageBox.Show("修改失败");
//this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
sCardText.Text = "";
sNameText.Text = "";
sAddressText.Text = "";
ageCombo.SelectedItem = 18;//设置默认值
nanBtn.Checked = true;//默认性别男
}
}
}
AddStaffForm.Designer.cs

namespace StaffManage
{
partial class AddStaffForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.sCardText = new System.Windows.Forms.TextBox();
this.ageCombo = new System.Windows.Forms.ComboBox();
this.sAddressText = new System.Windows.Forms.TextBox();
this.sNameText = new System.Windows.Forms.TextBox();
this.nanBtn = new System.Windows.Forms.RadioButton();
this.nvBtn = new System.Windows.Forms.RadioButton();
this.button1 = new System.Windows.Forms.Button();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.deptCombo = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(53, 52);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 15);
this.label1.TabIndex = 0;
this.label1.Text = "员工工号:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(367, 52);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(82, 15);
this.label2.TabIndex = 1;
this.label2.Text = "员工姓名:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(367, 101);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(82, 15);
this.label3.TabIndex = 2;
this.label3.Text = "员工性别:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(367, 149);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(82, 15);
this.label4.TabIndex = 3;
this.label4.Text = "员工年龄:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(53, 101);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(82, 15);
this.label5.TabIndex = 4;
this.label5.Text = "员工部门:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(53, 205);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(82, 15);
this.label6.TabIndex = 5;
this.label6.Text = "员工地址:";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(53, 149);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(82, 15);
this.label7.TabIndex = 6;
this.label7.Text = "员工工资:";
//
// sCardText
//
this.sCardText.Location = new System.Drawing.Point(141, 49);
this.sCardText.Name = "sCardText";
this.sCardText.Size = new System.Drawing.Size(178, 25);
this.sCardText.TabIndex = 7;
//
// ageCombo
//
this.ageCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ageCombo.FormattingEnabled = true;
this.ageCombo.Location = new System.Drawing.Point(446, 146);
this.ageCombo.Name = "ageCombo";
this.ageCombo.Size = new System.Drawing.Size(177, 23);
this.ageCombo.TabIndex = 9;
//
// sAddressText
//
this.sAddressText.Location = new System.Drawing.Point(141, 203);
this.sAddressText.Multiline = true;
this.sAddressText.Name = "sAddressText";
this.sAddressText.Size = new System.Drawing.Size(482, 56);
this.sAddressText.TabIndex = 12;
//
// sNameText
//
this.sNameText.Location = new System.Drawing.Point(446, 49);
this.sNameText.Name = "sNameText";
this.sNameText.Size = new System.Drawing.Size(178, 25);
this.sNameText.TabIndex = 13;
//
// nanBtn
//
this.nanBtn.AutoSize = true;
this.nanBtn.Location = new System.Drawing.Point(455, 99);
this.nanBtn.Name = "nanBtn";
this.nanBtn.Size = new System.Drawing.Size(43, 19);
this.nanBtn.TabIndex = 14;
this.nanBtn.TabStop = true;
this.nanBtn.Text = "男";
this.nanBtn.UseVisualStyleBackColor = true;
//
// nvBtn
//
this.nvBtn.AutoSize = true;
this.nvBtn.Location = new System.Drawing.Point(521, 99);
this.nvBtn.Name = "nvBtn";
this.nvBtn.Size = new System.Drawing.Size(43, 19);
this.nvBtn.TabIndex = 15;
this.nvBtn.TabStop = true;
this.nvBtn.Text = "女";
this.nvBtn.UseVisualStyleBackColor = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(270, 289);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 16;
this.button1.Text = "添加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(142, 146);
this.numericUpDown1.Maximum = new decimal(new int[] {
1000000,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(139, 25);
this.numericUpDown1.TabIndex = 17;
//
// deptCombo
//
this.deptCombo.FormattingEnabled = true;
this.deptCombo.Location = new System.Drawing.Point(142, 98);
this.deptCombo.Name = "deptCombo";
this.deptCombo.Size = new System.Drawing.Size(177, 23);
this.deptCombo.TabIndex = 18;
//
// button2
//
this.button2.Location = new System.Drawing.Point(432, 289);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 19;
this.button2.Text = "重置";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// AddStaffForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button2);
this.Controls.Add(this.deptCombo);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.button1);
this.Controls.Add(this.nvBtn);
this.Controls.Add(this.nanBtn);
this.Controls.Add(this.sNameText);
this.Controls.Add(this.sAddressText);
this.Controls.Add(this.ageCombo);
this.Controls.Add(this.sCardText);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "AddStaffForm";
this.Text = "添加/修改员工信息";
this.Load += new System.EventHandler(this.AddStaffForm_Load);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox sCardText;
private System.Windows.Forms.ComboBox ageCombo;
private System.Windows.Forms.TextBox sAddressText;
private System.Windows.Forms.TextBox sNameText;
private System.Windows.Forms.RadioButton nanBtn;
private System.Windows.Forms.RadioButton nvBtn;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.ComboBox deptCombo;
private System.Windows.Forms.Button button2;
}
}
View Code
6.部门管理窗口DeptForm

DeptForm.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StaffManage
{
public partial class DeptForm : Form
{
public DeptForm()
{
InitializeComponent();
}
private void DeptForm_Load(object sender, EventArgs e)
{
//初始化列表
setListView();
}
/// <summary>
/// 初始化部门列表
/// </summary>
private void setListView()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("select * from deptInfo");
//添加条件查询
if(deptNameText.Text.Trim().Length != 0)
{
sql += string.Format(" where deptName like '%{0}%'", deptNameText.Text.Trim());
}
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
MySqlDataReader dataReader = mySqlCommand.ExecuteReader();
while (dataReader.Read())
{
string deptId = dataReader["deptId"].ToString();
string deptName = dataReader["deptName"].ToString();
string deptShow = dataReader["deptShow"].ToString();
ListViewItem item = new ListViewItem(deptId);
item.SubItems.Add(deptName);
item.SubItems.Add(deptShow);
listView1.Items.Add(item);
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
/// <summary>
/// 部门查询功能
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
//需要先清空列表中的内容
listView1.Items.Clear();
setListView();
}
/// <summary>
/// 点击修改按钮
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
//弹出修改页面
AddDeptForm addDeptForm = new AddDeptForm();
//获取选中行的第一列值
addDeptForm.flag =Convert.ToInt32(listView1.SelectedItems[0].Text);
addDeptForm.MdiParent = this.MdiParent;
addDeptForm.Show();
//修改后刷新
addDeptForm.FormClosed += new FormClosedEventHandler(DeptForm_FormClosed);
}
private void DeptForm_FormClosed(object sender, FormClosedEventArgs e)
{
listView1.Items.Clear();
setListView();
}
/// <summary>
/// 删除操作
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("确定要删除吗【" + listView1.SelectedItems[0].SubItems[1].Text + "】吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dialogResult == DialogResult.Yes)
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("delete from deptInfo where deptId = {0}", Convert.ToInt32(listView1.SelectedItems[0].Text));
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int rs = mySqlCommand.ExecuteNonQuery();
if (rs == 0)
{
MessageBox.Show("删除失败");
}
else
{
MessageBox.Show("删除成功");
listView1.Items.Clear();
setListView();//刷新
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
}
}
}
DeptForm.Designer.cs

namespace StaffManage
{
partial class DeptForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.deptNameText = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.listView1 = new System.Windows.Forms.ListView();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.deptNameText);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(28, 22);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(525, 110);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "搜索条件";
//
// button1
//
this.button1.Location = new System.Drawing.Point(370, 39);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 25);
this.button1.TabIndex = 2;
this.button1.Text = "查询";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// deptNameText
//
this.deptNameText.Location = new System.Drawing.Point(119, 41);
this.deptNameText.Name = "deptNameText";
this.deptNameText.Size = new System.Drawing.Size(199, 25);
this.deptNameText.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(31, 44);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 15);
this.label1.TabIndex = 0;
this.label1.Text = "部门名称:";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.FullRowSelect = true;
this.listView1.HideSelection = false;
this.listView1.Location = new System.Drawing.Point(28, 152);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(525, 297);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// button2
//
this.button2.Location = new System.Drawing.Point(135, 462);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "修改";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 462);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "删除";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// columnHeader1
//
this.columnHeader1.Text = "部门编号";
this.columnHeader1.Width = 80;
//
// columnHeader2
//
this.columnHeader2.Text = "部门名称";
this.columnHeader2.Width = 100;
//
// columnHeader3
//
this.columnHeader3.Text = "部门介绍";
this.columnHeader3.Width = 345;
//
// DeptForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(581, 505);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.listView1);
this.Controls.Add(this.groupBox1);
this.Name = "DeptForm";
this.Text = "部门列表";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.DeptForm_FormClosed);
this.Load += new System.EventHandler(this.DeptForm_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox deptNameText;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
}
}
View Code
7.添加/修改部门信息AddDeptForm

AddDeptForm.cs
using MySql.Data.MySqlClient;
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;
namespace StaffManage
{
public partial class AddDeptForm : Form
{
public int flag = 0;
public AddDeptForm()
{
InitializeComponent();
}
private void AddDeptForm_Load(object sender, EventArgs e)
{
//判断是用来添加还是修改
if(flag == 0)
{
//添加
button1.Text = "添加";
}
else
{
//修改
button1.Text = "修改";
//初始化窗体数据
setDeptById();
}
}
/// <summary>
/// 初始化窗体数据
/// </summary>
private void setDeptById()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("select * from deptInfo where deptId = {0}", flag);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
if (mySqlDataReader.Read())
{
deptNameText.Text = mySqlDataReader["deptName"].ToString();
deptShowText.Text = mySqlDataReader["deptShow"].ToString();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
//非空认证
if (CheckInput())
{
//点击添加的时候,判断是添加还是修改
if (flag == 0)
{
//添加功能
insertDept();
}
else
{
//修改功能
updateDept();
}
}
}
private void insertDept()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("insert into deptInfo(`deptName`,`deptShow`) values('{0}','{1}')"
, deptNameText.Text.Trim()
, deptShowText.Text.Trim()
);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int result = mySqlCommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("添加成功");
this.Close();
}
else
{
MessageBox.Show("添加失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
private void updateDept()
{
MySqlConnection mySqlConnection = null;
try
{
mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course");
mySqlConnection.Open();
string sql = string.Format("update deptInfo set deptName='{0}',deptShow='{1}' where deptId={2}"
, deptNameText.Text.Trim()
, deptShowText.Text.Trim()
, flag);
MySqlCommand mySqlCommand = new MySqlCommand(sql, mySqlConnection);
int result = mySqlCommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("修改成功");
this.Close();
}
else
{
MessageBox.Show("修改失败");
//this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
finally
{
mySqlConnection.Close();
}
}
private Boolean CheckInput()
{
if (deptNameText.Text.Trim().Length == 0)
{
MessageBox.Show("部门名称不能为空");
deptNameText.Focus();
return false;
}
if (deptShowText.Text.Trim().Length == 0)
{
MessageBox.Show("员工姓名不能为空");
deptShowText.Focus();
return false;
}
return true;
}
private void button2_Click(object sender, EventArgs e)
{
deptNameText.Text = "";
deptShowText.Text = "";
}
}
}
AddDeptForm.Designer.cs

namespace StaffManage
{
partial class AddDeptForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.deptNameText = new System.Windows.Forms.TextBox();
this.deptShowText = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(46, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 15);
this.label1.TabIndex = 0;
this.label1.Text = "部门名称:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(46, 95);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(82, 15);
this.label2.TabIndex = 1;
this.label2.Text = "部门介绍:";
//
// deptNameText
//
this.deptNameText.Location = new System.Drawing.Point(134, 38);
this.deptNameText.Name = "deptNameText";
this.deptNameText.Size = new System.Drawing.Size(225, 25);
this.deptNameText.TabIndex = 2;
//
// deptShowText
//
this.deptShowText.Location = new System.Drawing.Point(134, 92);
this.deptShowText.Multiline = true;
this.deptShowText.Name = "deptShowText";
this.deptShowText.Size = new System.Drawing.Size(225, 77);
this.deptShowText.TabIndex = 3;
//
// button1
//
this.button1.Location = new System.Drawing.Point(134, 211);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "添加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(284, 211);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "重置";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// AddDeptForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(432, 303);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.deptShowText);
this.Controls.Add(this.deptNameText);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "AddDeptForm";
this.Text = "添加/修改部门";
this.Load += new System.EventHandler(this.AddDeptForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox deptNameText;
private System.Windows.Forms.TextBox deptShowText;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
View Code
8.帮助窗口HelpForm

HelpForm.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;
namespace StaffManage
{
public partial class HelpForm : Form
{
public HelpForm()
{
InitializeComponent();
}
}
}
HelpForm.Designer.cs

namespace StaffManage
{
partial class HelpForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 16.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(113, 116);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(186, 84);
this.label1.TabIndex = 0;
this.label1.Text = "员工管理系统\r\n\r\n更多信息..";
//
// HelpForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(419, 398);
this.Controls.Add(this.label1);
this.Name = "HelpForm";
this.Text = "关于我们";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
View Code