📄 deleteemployee.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace AssetManagement
{
/// <summary>
/// deleteEmployee 的摘要说明。
/// </summary>
public class deleteEmployee : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private string employeeNo;
bool check1 = false;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public deleteEmployee()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public deleteEmployee(string e)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.employeeNo = e;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 80);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "员工号";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(120, 80);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 80);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "删除";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(272, 168);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// deleteEmployee
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 214);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "deleteEmployee";
this.Text = "deleteEmployee";
this.Load += new System.EventHandler(this.deleteEmployee_Load);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("员工号不能为空!");
this.textBox1.Focus();
}
else
{
bool check = false;
Connection s = new Connection();
SqlConnection conn = new SqlConnection(s.getConnectionString);
conn.Open();
string str1 = "select * from employee";
SqlCommand cmd = new SqlCommand(str1,conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (this.textBox1.Text.Trim() == Convert.ToString(dr["employeeNo"]))
{
this.label2.Text = Convert.ToString(dr["employeeName"]);
check = true;
break;
}
}
dr.Close();
if (check)
{
string str2 = "delete employee where employeeNo = '" + this.textBox1.Text.Trim() + "'";
SqlCommand cmd1 = new SqlCommand(str2,conn);
try
{
cmd1.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
catch
{
MessageBox.Show("删除失败!");
}
}
else
{
MessageBox.Show("员工号不存在!");
this.textBox1.Focus();
this.textBox1.Text = "";
}
conn.Close();
}
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Trim().Length > 8)
{
MessageBox.Show("输入的编号无效!");
this.textBox1.Text = "";
this.textBox1.Focus();
}
else
{
Connection s = new Connection();
SqlConnection conn = new SqlConnection(s.getConnectionString);
conn.Open();
string str1 = "select * from employee";
SqlCommand cmd = new SqlCommand(str1,conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if ((this.textBox1.Text.Trim().ToUpper() == Convert.ToString(dr["employeeNo"]))&&(Convert.ToString(dr["employeeNo"]) != this.employeeNo.ToUpper()))
{
this.label2.Visible = true;
//this.label3.Visible = true;
this.label2.Text = Convert.ToString(dr["employeeName"]);
check1 = true;
break;
}
check1 = false;
}
dr.Close();
conn.Close();
if (!check1)
{
this.label2.Visible = false;
//this.label3.Visible = false;
this.button1.Enabled = false;
}
else
{
this.button1.Enabled = true;
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void deleteEmployee_Load(object sender, System.EventArgs e)
{
this.button1.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -