修改程序登录入口,管理员操作

一:经过两天的努力,今天终于解决了一系列让我头疼的问题,这些问题可很简单,写下来记住吧,解决方案资源管理器截图如下:

二:登录入口选择的代码,program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HYMSys.UI
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            login login = new login();
            login.ShowDialog();
            if (login.DialogResult == DialogResult.OK)//对话框的返回值为ok时运行HRMSys窗口
            {
                Application.Run(new HRMSys());
            }
            //Application.Run(new login());
        }
    }
}
View Code

登录的优化操作:

1.login窗口的AccoptButton属性为登录按钮,CancleButton为关闭按钮,这样回车直接登录了和用Esc键直接关闭

2.login窗口的MaximizeBox属性为:False

3.login窗口的StartPosition属性为:CenterScreen

三:OperatorListUI.cs代码,将管理员全部显示出来

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSys.DAL;
using HRMSys.Model;

namespace HYMSys.UI.SystemMgr
{
    public partial class OperatorLisrUI : Form
    {
        public OperatorLisrUI()
        {
            InitializeComponent();
        }
        OperatorEditUI edit = new OperatorEditUI();
        /// <summary>
        /// 刷新数据
        /// </summary>
        private void loadSource()
        {
            OperatorDAL ope = new OperatorDAL();
           
            dataGridView1.DataSource =ope.GetListNotDelete();//dataGridView1.DataSource可以直接接收一个object数组
 
        }
        /// <summary>
        ///窗口启动自动加载数据没有被软删除的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OperatorLisrUI_Load(object sender, EventArgs e)
        {

            loadSource();
         
        }
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_add_Click(object sender, EventArgs e)
        {
            
            edit.IsInsert = true;//不同窗口间传参数;
            edit.ShowDialog();
            if (edit.loadSouce == true)
            {
                loadSource();
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_delete_Click(object sender, EventArgs e)
        {
           
            OperatorDAL dal = new OperatorDAL();
            edit.EditingId = (Guid)dataGridView1.CurrentRow.Cells[0].Value;//取得id的值
            string name= (string)dataGridView1.CurrentRow.Cells[1].Value;
            if (MessageBox.Show("真的要删除--"+name+"--吗?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                dal.softDelete(edit.EditingId);
                edit.loadSouce = true;
                
            }
            if (edit.loadSouce == true)
            {
                loadSource();
            }
            

        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_edit_Click(object sender, EventArgs e)
        {
           
           
            edit.IsInsert = false;//不同窗口间传参数;

            //edit.EditingId = dataGridView1.Rows[2].Cells[].Value;
            edit.EditingId = (Guid)dataGridView1.CurrentRow.Cells[0].Value;//取得id的值
            edit.ShowDialog();
            if(edit.loadSouce==true)
            {
                loadSource();
            }
            

        }

       
    }
}
View Code

此过程中遇到的问题:

1.如何将datagridview中显示的是文字,而不是数据源的字段,只显示其中的三个字段,得到选中行的某单元格的内容如图:

a.将datagridview列的属性HeaderText设置为显示的文字,DataPropertyName设置为数据源对应的字段名,这就实现了如图用户名下显示的数据源的UserName字段,如图操作:

 

 

b.将数据源的所有字段都添加到datagridview列属性中,这样就不会出现自动添加以数据源字段为列属性了,将不用的列属性隐藏即可

c.dataGridView1.CurrentRow.Cells[0].Value;//得到当前选中行的第一列单元格的内容

三:OperatorDAL.cs的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace HRMSys.DAL
{
    public class OperatorDAL
    {
        /// <summary>
        /// 插入一个用户
        /// </summary>
        /// <param name="op"></param>
        public void insertUser(Operator op)
        {
            sqlhelper.ExecuteNon(@"insert into T_Operator (Id,UserName,Password,IsLocked,IsDelete,RealName) values (newid(),@UserName,@Password,0,0,@RealName)",
                                new SqlParameter ("@UserName",op.UserName),
                                new SqlParameter ("@Password",op.Password),
                                new SqlParameter("@RealName",op.RealName));
        }
        
        /// <summary>
        /// 查询是否存在用户名
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool checkNameRepeat(string name)
        {
            DataTable table = sqlhelper.datatable("select * from T_Operator where UserName=@UserName",
                new SqlParameter("@UserName", name));
            if (table.Rows.Count <= 0)
                return true;//不存在

            else 
                return false;//已存在

        }
        /// <summary>
        /// 将表的形式转换为vs的形式,给对象
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private Operator ToOperator(DataRow row)
        {
            Operator op = new Operator();
            op.Id = (Guid)row["Id"];//不加就可以检查用户名是否重复了
            op.UserName = (string)row["UserName"];
            op.Password = (string)row["Password"];
            op.IsLocked = (bool)row["IsLocked"];
            op.IsDelete=(bool)row["IsDelete"];
            op.RealName = (string)row["RealName"];
            return op;
        }
        /// <summary>
        ///查询指定username的一条数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Operator loginUser(string name)
        {
               DataTable table = sqlhelper.datatable("select * from T_Operator where UserName=@UserName and IsDelete=0 and IsLocked=0",
                   new SqlParameter("@UserName", name));
               if (table.Rows.Count <= 0)
                   return null;

               else if (table.Rows.Count > 1)
                   throw new Exception("用户名重复");
               else
               {
                   DataRow row = table.Rows[0];
                  

                   return ToOperator(row);
               }
        }
        /// <summary>
        /// 锁定管理员更新管理员表
        /// </summary>
        /// <param name="op"></param>
        public void UpdateOperator(Operator op)
        {
            sqlhelper.ExecuteNon("update T_Operator set IsLocked=@IsLocked  where Id=@Id",
                new SqlParameter("@IsLocked", op.IsLocked),
                new SqlParameter("@id",op.Id));
 
        }
        /// <summary>
        /// 根据id软删除一条数据
        /// </summary>
        /// <param name="id"></param>
        public void softDelete(Guid id)
        {
            sqlhelper.ExecuteNon("update T_Operator set IsDelete=1 where Id=@Id",
                new SqlParameter("@Id",id));
 
        }
        /// <summary>
        /// 得到没有软删除的所有数据
        /// </summary>
        /// <returns></returns>
        public Operator[] GetListNotDelete()
        {
            
            DataTable listOp=sqlhelper.datatable("select * from T_Operator where IsDelete=0");
            Operator[] op = new Operator[listOp.Rows.Count];
            for (int i = 0; i < listOp.Rows.Count; i++)
            {
                op[i] = ToOperator(listOp.Rows[i]);
            }
            return op;
        }
        //得到指定Id的一条数据
        public Operator GetOpById(Guid id)
        {

            DataTable table = new DataTable();
           
          table=  sqlhelper.datatable("select * from T_Operator where Id=@Id",
                new SqlParameter("@Id",id));

          if (table.Rows.Count <= 0)
              return null;

          else if (table.Rows.Count > 1)
              throw new Exception("ID重复");
          else
          {
              return ToOperator(table.Rows[0]);
          }
        

        }
        //更新一条数据
        public void updateTwoByid(string username,string realname,Guid id)
        {//两个set间要有逗号UserName=@UserName ,RealName=@RealName
            sqlhelper.ExecuteNon("update T_Operator set UserName=@UserName ,RealName=@RealName where Id=@Id",
                new SqlParameter("@UserName",username),
                new SqlParameter("@RealName",realname),
                new SqlParameter("@Id",id));
        }
        public void updateThreeByid(string username, string realname,string password,Guid id)
        {


            sqlhelper.ExecuteNon("update T_Operator set UserName=@UserName, RealName=@RealName,Password=@Password where Id=id",
                new SqlParameter("@UserName", username),
                new SqlParameter("@RealName", realname),
                new SqlParameter("@Password",password)
                );
        }
       
    }
}
View Code

四:CommonHelper.cs和App.config的代码,将密码盐放到App.config中,CommonHelper.cs取到密码盐

CommonHelper.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;

namespace HYMSys.UI
{
  class CommonHelper
    {
        /// <summary>
        /// MD5加密算法,将添加的用户名和密码加密
        /// </summary>
        /// <param name="sDataIn"></param>
        /// <returns></returns>
        public static string GetMD5(string sDataIn)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
            bytHash = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return sTemp.ToLower();
        }
      /// <summary>
      /// 取得密码盐
      /// </summary>
      /// <returns></returns>
        public static string getPasswordSalt()
        {
            string passwordSalt = ConfigurationManager.AppSettings["PasswodSalt"];
           return passwordSalt;
        }
    }
}
View Code

App.config的代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="constr" connectionString="server=.;database=HRMSysDB;uid=hrmsa;pwd=love@zhengzhou"/>
  </connectionStrings>
  <appSettings>
    <add key="PasswodSalt" value="Love@.>1"/>
  </appSettings>
</configuration>

五:OperatorEditUI.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSys.Model;
using HRMSys.DAL;

namespace HYMSys.UI.SystemMgr
{
    public partial class OperatorEditUI : Form
    {
        public OperatorEditUI()
        {
            InitializeComponent();
        }
        public bool IsInsert { get; set; }//是否插入
        public Guid EditingId { get; set; }//列表窗口的id,也是表的id
        public bool loadSouce { get; set; }//用来让编辑窗口关闭后刷新列表窗口
        /// <summary>
        /// 保存修改或插入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (IsInsert)
            {
                Operator opp = new Operator();
                OperatorDAL op = new OperatorDAL();
                opp.Password = CommonHelper.GetMD5(tb_password.Text + CommonHelper.getPasswordSalt());
                opp.UserName = tb_username.Text;
                opp.RealName = tb_realname.Text;
                op.insertUser(opp);
                MessageBox.Show("添加成功");



            }
            else
            {
                if (tb_password.Text.Length <= 0)
                {
                    OperatorDAL op = new OperatorDAL();
                    op.updateTwoByid(tb_username.Text, tb_realname.Text, EditingId);
                    MessageBox.Show("更改成功");

                }
                else
                {
                    string password = tb_password.Text;
                    password = CommonHelper.GetMD5(password + CommonHelper.getPasswordSalt());
                    OperatorDAL op = new OperatorDAL();
                    op.updateThreeByid(tb_username.Text, tb_realname.Text, password, EditingId);
                    MessageBox.Show("更改成功");


                }
            }



        }
        /// <summary>
        /// 自动载入指定id数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OperatorEditUI_Load(object sender, EventArgs e)
        {
            if (IsInsert)
            {


            }
            else
            {
                OperatorDAL opda = new OperatorDAL();
                Operator op = new Operator();
                op = opda.GetOpById(EditingId);
                tb_username.Text = op.UserName;
                tb_realname.Text = op.RealName;
            }
        }
        /// <summary>
        /// 窗口关闭,将datagridview重新载入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OperatorEditUI_FormClosing(object sender, FormClosingEventArgs e)
        {

            loadSouce = true;
        }
        /// <summary>
        /// 取消时关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_cancle_Click(object sender, EventArgs e)
        {
            loadSouce = false;
            this.Close();
           
           
        }
    }
}
View Code

六:更改应用程序的图标,在主程序上右键单击属性,得到如图界面,更改即可:

 

posted @ 2014-03-25 15:55  HongMaJu  阅读(689)  评论(2编辑  收藏  举报