一:解决方案资源管理器截图:
二:operatorDAL.cs代码
class="code_img_closed" src="/Upload/Images/2014032213/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('df9602c5-efe7-49cb-b92d-60fbfb2c1c44',event)" src="/Upload/Images/2014032213/2B1B950FA3DF188F.gif" alt="" />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) values (newid(),@UserName,@Password)", new SqlParameter ("@UserName",op.UserName), new SqlParameter ("@Password",op.Password)); } /// <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"]; 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", 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); } } } }View Code
三:commonhelper.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; 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(); } } }View Code
四:hrmsys.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 { public partial class HRMSys : Form { public HRMSys() { InitializeComponent(); } /// <summary> /// 添加一个用户 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 操作管理员ToolStripMenuItem_Click(object sender, EventArgs e) { //MD5将要添加的用户名和密码加密 Operator op = new Operator(); op.UserName = CommonHelper.GetMD5( "hmj"+"Love@.>1"); op.Password = CommonHelper.GetMD5("123" + "Love@.>1"); OperatorDAL opDal = new OperatorDAL(); opDal.insertUser(op); } } }View Code
五:login.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 { public partial class login : Form { public login() { InitializeComponent(); } /// <summary> /// 登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { OperatorDAL opDAL=new OperatorDAL(); //无法直接调用sqlhelper,通过operatorDAL的方法掉用sqlhelper string name=CommonHelper.GetMD5(tb_name.Text + "Love@.>1"); string password=CommonHelper.GetMD5(tb_pwd.Text+ "Love@.>1"); Operator op = opDAL.loginUser(name); if (op == null) MessageBox.Show("用户名不存在"); else if (op.Password != password) MessageBox.Show("用户名或密码错误"); else { HRMSys form = new HRMSys(); form.Show(); this.Hide(); } } } }View Code