In computing, CRUD is an acronym for create, retrieve, update, and delete. It is used to refer to the basic functions of a database or persistence layer in a software system.
C reate new records
R etrieve existing records
U pdate existing records
D elete existing records.
在进入主界面后,点击员工设置;选择添加员工:
private
void tsmiAdd_Click(
object sender, EventArgs e)
{
new FrmDealEmployee( "添加员工信息", null, this).ShowDialog();
}
{
new FrmDealEmployee( "添加员工信息", null, this).ShowDialog();
}
为增强代码的重用性,添加员工与修改员工共用窗体信息,如果添加员工标签被激发,则该窗体的label显示:
添加员工信息
;因为修改员工信息与新增功能相似,只介绍其中一个。
实现新增员工的代码如下:
Employee emp =
new Employee();
if (txtEMPID.Text == "" || txtEmpName.Text == "" || txtMobilePhone.Text == "" ||
txtPhone.Text == "" || txtEmpCertType.Text == "" ||
txtEmpCertNum.Text == "" || txtDiscount.Text == "" ||
txtAddress.Text == "")
{
MessageBox.Show( "填写完整信息", "提示");
return;
}
float discount = 0.0f;
try
{
float.TryParse(txtDiscount.Text.Trim(), out discount);
}
catch (Exception ee)
{
MessageBox.Show( "折扣率错误", "提示");
return;
}
emp.EmpID = NumberFormat.ConvertToFixLenString(txtEMPID.Text.Trim(), IDCode.EmployeeIDLength);
emp.Password = EnCriptPassword.MD5EnCriptPassword(emp.EmpID);
emp.UserRightCode = ""; //curemp.UserRightCode;权限
emp.EmpLevel = 0;
emp.RoleID = ( int)cmbRole.SelectedValue;
emp.DepartmentID = ( short)1; // cmbDepartment.SelectedValue;
emp.EmpCertNum = txtEmpCertNum.Text.Trim();
emp.EmpCertType = txtEmpCertType.Text.Trim();
emp.EmpDiscount = discount;
emp.EmpAddress = txtAddress.Text.Trim();
emp.EmpMobile = txtMobilePhone.Text.Trim();
emp.EmpName = txtEmpName.Text.Trim();
emp.EmpPhone = txtPhone.Text.Trim();
emp.UserBirthDate = dtpBirthDay.Value;
if (EmployeeBLL.AddNewEmp(emp))
{
MessageBox.Show( "修改成功", "提示");
this.parent.RefreshEmpList(); //更新父窗体的信息
this.Close();
}
else
{
MessageBox.Show( "修改失败", "提示");
}
}
if (txtEMPID.Text == "" || txtEmpName.Text == "" || txtMobilePhone.Text == "" ||
txtPhone.Text == "" || txtEmpCertType.Text == "" ||
txtEmpCertNum.Text == "" || txtDiscount.Text == "" ||
txtAddress.Text == "")
{
MessageBox.Show( "填写完整信息", "提示");
return;
}
float discount = 0.0f;
try
{
float.TryParse(txtDiscount.Text.Trim(), out discount);
}
catch (Exception ee)
{
MessageBox.Show( "折扣率错误", "提示");
return;
}
emp.EmpID = NumberFormat.ConvertToFixLenString(txtEMPID.Text.Trim(), IDCode.EmployeeIDLength);
emp.Password = EnCriptPassword.MD5EnCriptPassword(emp.EmpID);
emp.UserRightCode = ""; //curemp.UserRightCode;权限
emp.EmpLevel = 0;
emp.RoleID = ( int)cmbRole.SelectedValue;
emp.DepartmentID = ( short)1; // cmbDepartment.SelectedValue;
emp.EmpCertNum = txtEmpCertNum.Text.Trim();
emp.EmpCertType = txtEmpCertType.Text.Trim();
emp.EmpDiscount = discount;
emp.EmpAddress = txtAddress.Text.Trim();
emp.EmpMobile = txtMobilePhone.Text.Trim();
emp.EmpName = txtEmpName.Text.Trim();
emp.EmpPhone = txtPhone.Text.Trim();
emp.UserBirthDate = dtpBirthDay.Value;
if (EmployeeBLL.AddNewEmp(emp))
{
MessageBox.Show( "修改成功", "提示");
this.parent.RefreshEmpList(); //更新父窗体的信息
this.Close();
}
else
{
MessageBox.Show( "修改失败", "提示");
}
}
EmplyeeDAL.cs关于新增员工的功能如下:
public
static
bool AddNewEmp(Employee emp)
{
if (emp == null)
{
return false;
}
string sql = string.Format( "insert into employee(EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone,"+
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount) "+
"values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},{8},'{9}','{10}',{11},'{12}',{13})",
emp.EmpID,emp.EmpName,emp.EmpCertType,emp.EmpCertNum,emp.EmpPhone,emp.EmpMobile,emp.EmpAddress,
emp.EmpLevel,emp.DepartmentID,emp.Password,emp.UserRightCode,emp.RoleID,emp.UserBirthDate, emp.EmpDiscount);
return ExecuteCommand(sql) ;
}
{
if (emp == null)
{
return false;
}
string sql = string.Format( "insert into employee(EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone,"+
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount) "+
"values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},{8},'{9}','{10}',{11},'{12}',{13})",
emp.EmpID,emp.EmpName,emp.EmpCertType,emp.EmpCertNum,emp.EmpPhone,emp.EmpMobile,emp.EmpAddress,
emp.EmpLevel,emp.DepartmentID,emp.Password,emp.UserRightCode,emp.RoleID,emp.UserBirthDate, emp.EmpDiscount);
return ExecuteCommand(sql) ;
}
删除员工的界面代码:
private
void tsmiDelete_Click(
object sender, EventArgs e)
{
if (dgrvEmp.SelectedRows.Count < 1)
{
MessageBox.Show( "选择待修改的员工", "提示");
return;
}
if (MessageBox.Show( "要删除选中的员工吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
int num = 0;
for ( int i = 0; i < dgrvEmp.SelectedRows.Count; i++)
{
string empid = dgrvEmp.SelectedRows[i].Cells[ "empid"].Value.ToString();
if (EmployeeBLL.DeleteEmp(empid))
{
num++;
}
}
RefreshEmpList(); //更新列表
MessageBox.Show( string.Format( "选中{0}个员工,{1}个删除成功", dgrvEmp.SelectedRows.Count, num), "提示");
}
{
if (dgrvEmp.SelectedRows.Count < 1)
{
MessageBox.Show( "选择待修改的员工", "提示");
return;
}
if (MessageBox.Show( "要删除选中的员工吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
int num = 0;
for ( int i = 0; i < dgrvEmp.SelectedRows.Count; i++)
{
string empid = dgrvEmp.SelectedRows[i].Cells[ "empid"].Value.ToString();
if (EmployeeBLL.DeleteEmp(empid))
{
num++;
}
}
RefreshEmpList(); //更新列表
MessageBox.Show( string.Format( "选中{0}个员工,{1}个删除成功", dgrvEmp.SelectedRows.Count, num), "提示");
}
DAL代码(删除功能):
///
/// 删除员工
///
/// "empid">
///
public static bool DeleteEmp( string empid)
{
string sql = "delete from employee where empid = '" +empid + "'";
return ExecuteCommand(sql);
}
/// 删除员工
///
/// "empid">
///
public static bool DeleteEmp( string empid)
{
string sql = "delete from employee where empid = '" +empid + "'";
return ExecuteCommand(sql);
}
DAL中执行SQL语句:
///
/// 执行Sql 命令(增加删除修改)
///
/// "sql">
///
protected static bool ExecuteCommand( string sql)
{
int re = 0;
bool bl;
try
{
re = DBAccess.updateRecords(sql);
}
catch (SqlException sqle)
{
re = 0;
}
finally
{
bl = (re == 0 ? false : true);
}
return bl;
}
/// 执行Sql 命令(增加删除修改)
///
/// "sql">
///
protected static bool ExecuteCommand( string sql)
{
int re = 0;
bool bl;
try
{
re = DBAccess.updateRecords(sql);
}
catch (SqlException sqle)
{
re = 0;
}
finally
{
bl = (re == 0 ? false : true);
}
return bl;
}
获取员工列表和员工详细信息:
///
/// 获取员工列表
///
///
public static DataTable GetAllEmployee()
{
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone,"+
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount from employee";
DataTable dt = DBAccess.getDataTable(sql);
return dt;
}
public static List GetAllEmployeeList()
{
List lst =
new List();
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone," +
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount from employee";
SqlDataReader reader = DBAccess.getDataReader(sql);
while (reader.Read())
{
Employee emp = new Employee();
emp.EmpID =reader.IsDBNull(0)?"": reader.GetString(0);
emp.EmpName = reader.IsDBNull(1) ? "" : reader.GetString(1);
emp.EmpCertType = reader.IsDBNull(2) ? "" : reader.GetString(2);
emp.EmpCertNum = reader.IsDBNull(3) ? "" : reader.GetString(3);
emp.EmpPhone = reader.IsDBNull(4) ? "" : reader.GetString(4);
emp.EmpMobile = reader.IsDBNull(5) ? "" : reader.GetString(5);
emp.EmpAddress = reader.IsDBNull(6) ? "" : reader.GetString(6);
emp.EmpLevel = reader.IsDBNull(7) ? ( short)0 : reader.GetInt16(7);
emp.DepartmentID = reader.IsDBNull(8) ? ( short)0 : reader.GetInt16(8);
emp.Password = reader.IsDBNull(9) ? "" : reader.GetString(9);
emp.UserRightCode = reader.IsDBNull(10) ? "" : reader.GetString(10);
emp.RoleID = reader.IsDBNull(11) ? 0 : reader.GetInt32(11);
emp.UserBirthDate = reader.GetDateTime(12);
emp.EmpDiscount = reader.IsDBNull(13) ? ( float)0 : ( float)reader.GetDouble(13);
lst.Add(emp);
}
//DataTable dt = DBAccess.getDataTable(sql);
return lst;
}
///
/// 获取员工的详细信息
///
///
public static List GetAllEmpDetailList()
{
List lst =
new List();
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone," +
"EmpMobile,EmpAddress,EmpLevel,employee.DepartmentID,Password,UserRightCode,employee.RoleId,UserBirthDate,EmpDiscount,checkdepartname,rolename from employee,role,checkDepart" +
" where employee.roleid = role.roleid and employee.departmentid = checkdepart.checkdepartid";
SqlDataReader reader = DBAccess.getDataReader(sql);
while (reader.Read())
{
EmployeeDetail emp = new EmployeeDetail();
emp.EmpID = reader.IsDBNull(0) ? "" : reader.GetString(0);
emp.EmpName = reader.IsDBNull(1) ? "" : reader.GetString(1);
emp.EmpCertType = reader.IsDBNull(2) ? "" : reader.GetString(2);
emp.EmpCertNum = reader.IsDBNull(3) ? "" : reader.GetString(3);
emp.EmpPhone = reader.IsDBNull(4) ? "" : reader.GetString(4);
emp.EmpMobile = reader.IsDBNull(5) ? "" : reader.GetString(5);
emp.EmpAddress = reader.IsDBNull(6) ? "" : reader.GetString(6);
emp.EmpLevel = reader.IsDBNull(7) ? ( short)0 : reader.GetInt16(7);
emp.DepartmentID = reader.IsDBNull(8) ? ( short)0 : reader.GetInt16(8);
emp.Password = reader.IsDBNull(9) ? "" : reader.GetString(9);
emp.UserRightCode = reader.IsDBNull(10) ? "" : reader.GetString(10);
emp.RoleID = reader.IsDBNull(11) ? 0 : reader.GetInt32(11);
emp.UserBirthDate = reader.GetDateTime(12);
emp.EmpDiscount = reader.IsDBNull(13) ? ( float)0 : ( float)reader.GetDouble(13);
emp.DepartName = reader.IsDBNull(14) ? "" : reader.GetString(14);
emp.RoleName = reader.IsDBNull(15) ? "" : reader.GetString(15);
lst.Add(emp);
}
//DataTable dt = DBAccess.getDataTable(sql);
return lst;
}
/// 获取员工列表
///
///
public static DataTable GetAllEmployee()
{
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone,"+
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount from employee";
DataTable dt = DBAccess.getDataTable(sql);
return dt;
}
public static List
{
List
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone," +
"EmpMobile,EmpAddress,EmpLevel,DepartmentID,Password,UserRightCode,RoleId,UserBirthDate,EmpDiscount from employee";
SqlDataReader reader = DBAccess.getDataReader(sql);
while (reader.Read())
{
Employee emp = new Employee();
emp.EmpID =reader.IsDBNull(0)?"": reader.GetString(0);
emp.EmpName = reader.IsDBNull(1) ? "" : reader.GetString(1);
emp.EmpCertType = reader.IsDBNull(2) ? "" : reader.GetString(2);
emp.EmpCertNum = reader.IsDBNull(3) ? "" : reader.GetString(3);
emp.EmpPhone = reader.IsDBNull(4) ? "" : reader.GetString(4);
emp.EmpMobile = reader.IsDBNull(5) ? "" : reader.GetString(5);
emp.EmpAddress = reader.IsDBNull(6) ? "" : reader.GetString(6);
emp.EmpLevel = reader.IsDBNull(7) ? ( short)0 : reader.GetInt16(7);
emp.DepartmentID = reader.IsDBNull(8) ? ( short)0 : reader.GetInt16(8);
emp.Password = reader.IsDBNull(9) ? "" : reader.GetString(9);
emp.UserRightCode = reader.IsDBNull(10) ? "" : reader.GetString(10);
emp.RoleID = reader.IsDBNull(11) ? 0 : reader.GetInt32(11);
emp.UserBirthDate = reader.GetDateTime(12);
emp.EmpDiscount = reader.IsDBNull(13) ? ( float)0 : ( float)reader.GetDouble(13);
lst.Add(emp);
}
//DataTable dt = DBAccess.getDataTable(sql);
return lst;
}
///
/// 获取员工的详细信息
///
///
public static List
{
List
string sql = "select EmpID,EmpName,EmpCertType,EmpCertNum,EmpPhone," +
"EmpMobile,EmpAddress,EmpLevel,employee.DepartmentID,Password,UserRightCode,employee.RoleId,UserBirthDate,EmpDiscount,checkdepartname,rolename from employee,role,checkDepart" +
" where employee.roleid = role.roleid and employee.departmentid = checkdepart.checkdepartid";
SqlDataReader reader = DBAccess.getDataReader(sql);
while (reader.Read())
{
EmployeeDetail emp = new EmployeeDetail();
emp.EmpID = reader.IsDBNull(0) ? "" : reader.GetString(0);
emp.EmpName = reader.IsDBNull(1) ? "" : reader.GetString(1);
emp.EmpCertType = reader.IsDBNull(2) ? "" : reader.GetString(2);
emp.EmpCertNum = reader.IsDBNull(3) ? "" : reader.GetString(3);
emp.EmpPhone = reader.IsDBNull(4) ? "" : reader.GetString(4);
emp.EmpMobile = reader.IsDBNull(5) ? "" : reader.GetString(5);
emp.EmpAddress = reader.IsDBNull(6) ? "" : reader.GetString(6);
emp.EmpLevel = reader.IsDBNull(7) ? ( short)0 : reader.GetInt16(7);
emp.DepartmentID = reader.IsDBNull(8) ? ( short)0 : reader.GetInt16(8);
emp.Password = reader.IsDBNull(9) ? "" : reader.GetString(9);
emp.UserRightCode = reader.IsDBNull(10) ? "" : reader.GetString(10);
emp.RoleID = reader.IsDBNull(11) ? 0 : reader.GetInt32(11);
emp.UserBirthDate = reader.GetDateTime(12);
emp.EmpDiscount = reader.IsDBNull(13) ? ( float)0 : ( float)reader.GetDouble(13);
emp.DepartName = reader.IsDBNull(14) ? "" : reader.GetString(14);
emp.RoleName = reader.IsDBNull(15) ? "" : reader.GetString(15);
lst.Add(emp);
}
//DataTable dt = DBAccess.getDataTable(sql);
return lst;
}