码迷,mamicode.com
首页 > 其他好文 > 详细

人事工资信息管理系统(核心代码实现)

时间:2020-05-09 20:45:58      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:date   jin   type   提示   数据   ==   index   ica   dialog   

提供连接数据库,执行insert、delete、update的方法

 /// <summary>
        /// 执行insert、delete、update的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
        {

	using (SqlConnection conn = new SqlConnection(connStr))
	            {
	                conn.Open();
	                using (SqlCommand cmd = conn.CreateCommand())
	                {
	                    cmd.CommandText = sql;
	                    cmd.Parameters.AddRange(pms);
	                    return cmd.ExecuteNonQuery();
	                }
	            }
        }

 

XML文件操作代码

 /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name="strXMLPath"></param>
        public static void CreateXML(string strXMLPath)
        {
            XmlDocument xmlDoc = new XmlDocument();//新建一个xml文档
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));//xml文件头
            XmlNode root = xmlDoc.CreateElement("root");
            xmlDoc.AppendChild(root);
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "UserName", null);
            node.InnerText = "";
            root.AppendChild(node);
            xmlDoc.Save(strXMLPath);
        }

/// <summary>
        /// 写入数据,并保存
        /// </summary>
        /// <param name="strXMLPath"></param>
        /// <param name="strNodeName">写入的节点</param>
        /// <param name="strValue">写入的数据</param>
        public static void UpdateXml(string strXMLPath,string strNodeName, string strValue)
        {
            XmlDocument xmlDoc = new XmlDocument();//新建一个xml文档
            xmlDoc.Load(strXMLPath);//加载文件路径
            XmlNode root = xmlDoc.SelectSingleNode("root");
            XmlNode node = root.SelectSingleNode(strNodeName);
            node.InnerText = strValue;
            xmlDoc.Save(strXMLPath);
        }

/// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="strXMLPath"></param>
        /// <param name="nodeName">待读取的节点</param>
        /// <returns>返回节点对应的值</returns>
        public static string ReadXml(string strXMLPath,string nodeName)
        {
            XmlDocument xmlDoc = new XmlDocument();//新建一个xml文档
            xmlDoc.Load(strXMLPath);//加载文件路径
            XmlNode root = xmlDoc.SelectSingleNode("root");
            XmlNode node = root.SelectSingleNode(nodeName);
            return node.InnerText;
        }

 

日志操作类
 /// <summary>
    /// 日志操作类
    /// </summary>
    public static class myLog
    {
        /// <summary>
        /// 日志文件路径
        /// </summary>
        private static string strLogPath = Directory.GetCurrentDirectory() + @"\Log\日志文件" + DateTime.Today.ToString("yyyyMMdd") + ".log";
        /// <summary>
        /// 记录开始-用于开始执行代码时写入时间和开始
        /// </summary>
        /// <param name="strTitle">日志开始</param>
        public static string WriteStartLog(string strTitle)
        {
            string strRZ = "";
            //DateTime.Today.ToString("yyyyMMdd") + ".log"
            //判断是否存在文件
            if (File.Exists(strLogPath))//文件存在
            {
                //追加写入
                try
                {
                    using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                    {
                        strRZ += "开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                        strRZ += "开始:" + strTitle + Environment.NewLine;

                        sw.WriteLine("开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                        sw.WriteLine("开始:" + strTitle);
                        //sw.WriteLine("结束:" + strContent);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else//文件不存在
            {
                try
                {
                    //创建文件
                    Directory.CreateDirectory(Path.GetDirectoryName(strLogPath));
                    using (File.Create(strLogPath))
                    { }
                    //追加写入
                    //追加写入
                    using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                    {
                        strRZ += "开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                        strRZ += "开始:" + strTitle + Environment.NewLine;

                        sw.WriteLine("开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                        sw.WriteLine("开始:" + strTitle);
                        //sw.WriteLine("结束:" + strContent);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return strRZ;
        }
        /// <summary>
        /// 记录开始-用于开始执行代码时写入时间和开始
        /// </summary>
        /// <param name="strTitle">日志开始</param>
        /// <param name="sender">位置</param>
        public static string WriteStartLog(string strTitle, object sender)
        {
            string strRZ = "";

            string strSender = "";
            if (sender == null)
            { strSender = ""; }
            else
            { strSender = sender.ToString(); }
            //DateTime.Today.ToString("yyyyMMdd") + ".log"
            //判断是否存在文件
            if (File.Exists(strLogPath))//文件存在
            {
                //追加写入
                try
                {
                    using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                    {
                        strRZ += "位置:" + strSender + Environment.NewLine;
                        strRZ += "开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                        strRZ += "开始:" + strTitle + Environment.NewLine;

                        sw.WriteLine("位置:" + strSender);
                        sw.WriteLine("开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                        sw.WriteLine("开始:" + strTitle);
                        //sw.WriteLine("结束:" + strContent);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else//文件不存在
            {
                try
                {

                    //创建文件
                    Directory.CreateDirectory(Path.GetDirectoryName(strLogPath));
                    using (File.Create(strLogPath))
                    { }
                    //追加写入
                    //追加写入
                    using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                    {
                        strRZ += "位置:" + strSender + Environment.NewLine;
                        strRZ += "开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + strSender + Environment.NewLine;
                        strRZ += "开始:" + strTitle + Environment.NewLine;

                        sw.WriteLine("位置:" + strSender);
                        sw.WriteLine("开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                        sw.WriteLine("开始:" + strTitle);
                        //sw.WriteLine("结束:" + strContent);
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return strRZ;
        }

        /// <summary>
        /// 记录完成-用于记录代码执行完成后记录结束和结束时的时间
        /// </summary>
        /// <param name="strContent">结束</param>
        public static string WriteEndLog(string strContent)
        {
            string strRZ = "";

            try
            {
                using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                {
                    strRZ += "结束:" + strContent + Environment.NewLine;
                    strRZ += "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                    strRZ += Environment.NewLine;

                    sw.WriteLine("结束:" + strContent);
                    sw.WriteLine("结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                    sw.WriteLine(Environment.NewLine);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return strRZ;
        }

        /// <summary>
        /// 记录完成-用于记录代码执行完成后记录结束和结束时的时间
        /// </summary>
        /// <param name="ex">错误信息</param>
        public static string WriteEndLog(Exception ex)
        {
            string strRZ = "";

            try
            {
                using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                {
                    strRZ += "结束:" + ex.ToString() + Environment.NewLine;
                    strRZ += "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                    strRZ += Environment.NewLine;

                    sw.WriteLine("结束:" + ex.ToString());
                    sw.WriteLine("结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                    sw.WriteLine(Environment.NewLine);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return strRZ;
        }
        /// <summary>
        /// 记录完成-用于记录代码执行完成后记录结束和结束时的时间
        /// </summary>
        /// <param name="strContent">结束</param>
        /// <param name="ex">错误信息</param>
        public static string WriteEndLog(string strContent, Exception ex)
        {
            string strRZ = "";

            try
            {
                using (StreamWriter sw = new StreamWriter(strLogPath, true, Encoding.Default))
                {
                    strRZ += "结束:" + strContent + " 错误信息:" + ex.ToString() + Environment.NewLine;
                    strRZ += "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :" + Environment.NewLine;
                    strRZ += Environment.NewLine;

                    sw.WriteLine("结束:" + strContent + " 错误信息:" + ex.ToString());
                    sw.WriteLine("结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " :");
                    sw.WriteLine(Environment.NewLine);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return strRZ;
        }


        /// <summary>
        /// 获取距离今天的前几天日期
        /// </summary>
        /// <param name="beforeDay">前几天数</param>
        /// <returns>前几天的日期</returns>
        private static DateTime GetCulDateBefore(int beforeDay)
        {
            DateTime dt = DateTime.Now;
            string strBeforeDay = dt.AddDays(-beforeDay).ToString("yyyy-MM-dd");
            DateTime newDay = Convert.ToDateTime(strBeforeDay);
            return newDay;
        }

        /// <summary>
        /// 删除指定距离某天前的日志
        /// </summary>
        /// <param name="strLogPathes"></param>
        /// <param name="beforeDay"></param>
        /// <returns></returns>
        public static void DeleteLogByCulDateBefore(int beforeDay)
        {
            DateTime dtime = GetCulDateBefore(beforeDay);
            int strDate = Convert.ToInt32(dtime.ToString("yyyyMMdd"));
            //string[] stTempLogPathes = strLogPathes;
            string[] strLogPathes = Directory.GetFiles(Directory.GetParent(strLogPath).FullName, "*.log");
            for (int i = 0; i < strLogPathes.Length; i++)
            {
                string tempPath = strLogPathes[i];
                string logName = Path.GetFileNameWithoutExtension(tempPath);
                try
                {
                    int intLog = Convert.ToInt32(logName.Substring(4));
                    if (strDate > intLog)
                    {
                        WriteStartLog("删除日志");
                        File.Delete(tempPath);
                        WriteEndLog("删除日志成功,路径:" + tempPath);
                    }
                }
                catch (Exception ex)
                {
                    WriteEndLog("删除日志失败,路径:" + tempPath, ex);
                }
            }
        }
    }

 

验证手机号合法性

 /// <summary>
        /// 判断输入的字符串是否是一个合法的手机号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool CheckPhoneNoIsLegal(string phone)
        {
            phone = phone.Trim();    //    去除手机号的前后空格
            int phoneLength = phone.Length;    //    获取手机号的长度
            bool phoneCheck = Regex.IsMatch(phone, @"^[1]+[3-8]+\d{9}");
            bool phoneCheck2 = Regex.IsMatch(phone, @"^[1]+[9][1,8,9]+\d{8}");
            if (!phoneCheck && !phoneCheck2 || phoneLength != 11)
            {
                return false;    //    手机号不合法
            }
            return true;    //    手机号合法
        }

还有验证身份证、邮箱等的合法性,这里就不贴出来了,有需要的联系我。

 

数据库备份与还原

技术图片技术图片

 

 实现代码

 private void btnPath_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "bak文件|*.bak";
            sfd.AddExtension = true;
            sfd.Title = "备份数据库";
            if(sfd.ShowDialog()==DialogResult.OK)
            {
                string strLuJing = sfd.FileName;//获取路径
                txtBackUpPath.Text = strLuJing;//赋值给文本显示
            }
        }

        private void btnBackUp_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否备份数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                if (txtBackUpPath.Text.ToString() != "")
                {
                    bool isBackUped = SQLHelper.BackUpDataBase(txtBackUpPath.Text);
                    if(isBackUped==true)
                    {
                        MessageBox.Show("备份完成!");
                    }
                    else
                    {
                        MessageBox.Show("备份失败!");
                    }
                }
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            //文件控件
            OpenFileDialog filename = new OpenFileDialog();
            //获取路径
            filename.InitialDirectory = Application.StartupPath;
            filename.Multiselect = false;
            //设置可打开的文件格式
            filename.Filter = "bak文件|*.bak";
            if (filename.ShowDialog() == DialogResult.OK)
            {
                txtRestore.Text = filename.FileName;
            }
        }

        private void btnExquteRestore_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否还原数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                if (txtRestore.Text.ToString() != "")
                {
                    string databasefile = txtRestore.Text;
                    bool isRestore = SQLHelper.RestoreDataBase(databasefile);
                    if(isRestore==true)
                    {
                        MessageBox.Show("还原完成!");
                    }
                    else
                    {
                        MessageBox.Show("还原失败!");
                    }
                }
            }
        }

 

实现对数据库的增删改查

//增加和修改
            if (isEdit == false)//新增保存
            {
                ProjectTable temp = new ProjectTable();
                ControlsDataToModel(temp);
              
                try
                {
                    myLog.WriteStartLog("新增项目信息", sender);

                    int isAddNew = new ProjectTableBLL().AddNew(temp);
                    MessageBox.Show("新增成功!");
                    InitGetData();

                    myLog.WriteEndLog("新增项目信息完成");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("失败!请查看操作日志,联系管理员解决!");
                    myLog.WriteEndLog("新增项目信息失败", ex);
                }
            }
            else if(isEdit== true)//编辑保存
            {
                ControlsDataToModel(projectTable);
                
                try
                {
                    myLog.WriteStartLog("编辑保存项目信息", sender);
                    bool isUpdate = new ProjectTableBLL().Update(projectTable);
                    MessageBox.Show("保存成功!");
                    InitGetData();

                    myLog.WriteEndLog("编辑保存项目信息完成");
                }
               catch (Exception ex)
                {
                    MessageBox.Show("失败!请查看操作日志,联系管理员解决!");
                    myLog.WriteEndLog("编辑保存项目信息失败", ex);
                }
            }


//删除
           DialogResult drt=  MessageBox.Show("确定要删除该条目吗?删除后无法恢复!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            if (drt==DialogResult.Yes)
            {
                try
                {
                    myLog.WriteStartLog("删除项目信息", sender);
                    bool isDelete = new ProjectTableBLL().Delete(projectTable.XM_ID);
                    InitGetData();
                    myLog.WriteEndLog("删除项目信息完成");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("失败!请查看操作日志,联系管理员解决!");
                    myLog.WriteEndLog("删除项目信息失败", ex);
                }
            }

//查询
 if (txtCheckByXM_MC.EditValue == null)
            {
                MessageBox.Show("请输入名称!");
                return;
            }
            try
            {
                myLog.WriteStartLog("根据项目名称查询项目信息", sender);

                DataTable dt = new ProjectTableBLL().GetByName(txtCheckByXM_MC.EditValue.ToString());
                gridControl1.DataSource = dt;
                selIndex = 0;
                getSelDataIndex(selIndex);
                ControlsEnable(false);

                myLog.WriteEndLog("根据项目名称查询项目信息完成");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("失败!请查看操作日志,联系管理员解决!");
                myLog.WriteEndLog("根据项目名称查询项目信息失败", ex);
            }

 

 

 

就不一 一的贴出来了,需要的联系我,共同交流,软件源码也好商量。

tel:18798835893

 

人事工资信息管理系统(核心代码实现)

标签:date   jin   type   提示   数据   ==   index   ica   dialog   

原文地址:https://www.cnblogs.com/ArcGISEngine201907121013/p/12859805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!