c#实现增删查改操作

时间:2021-01-29 16:17:18
           CRUD 是指在做计算处理时的增加 (Create) 、查询 (Retrieve) (重新得到数据)、更新 (Update) 和删除 (Delete) 几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能。

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

本文出自 “江南矿工技术空间” 博客,请务必保留此出处http://jncumter.blog.51cto.com/812546/232414