系统功能概述:
主要功能实现人事信息的增删查改。细分如下:
1.实现管理员登录和注册。
2.实现管理员修改密码
3.实现对公司人员信息的添加
4.实现对公司人员信息的修改
5.实现对公司人员信息的查询,模糊查询和精确查询
6.实现对公司人员信息的删除
实现技术
C#+sqlserver+三层架构
注册
该功能为管理员注册账号,设计有一个密码两次确认,密码存储为MD5加密,以及基本的输入数据格式验证的功能设计。
如下为核心代码:
//MD5加密
public static string ToMD5(string source)
{
StringBuilder sb = new StringBuilder();
MD5 md5 = MD5.Create();
byte[] data = Encoding.UTF8.GetBytes(source);
data = md5.ComputeHash(data);
foreach (var item in data)
{
sb.Append(item.ToString("x2"));
}
return sb.ToString();
}
//注册实现
private void btnSubmit_Click(object sender, EventArgs e)
{
string count = txt_count.Text;
string pwd =ToMD5(txt_pwd.Text);
string nc = txt_name.Text;
string rePwd = ToMD5(txt_submit.Text);
if (count == "" || pwd == "" || nc == "" || rePwd == "")
{
MessageBox.Show("请输入完整信息!!!");
return;
}
if (!ver.IsCode(txt_count.Text)) //验证账号格式是否正确4-16位数字
{
MessageBox.Show("格式不正确,请输入非0开头的4-16位数字");
return;
}
if (pwd == rePwd)
{
Models.Login user = new Models.Login { account = count, password = pwd, name = nc };
bool result = ma.Register(user);
if (result)
{
MessageBox.Show("注册成功!!!");
txt_count.Text = "";
txt_pwd.Text="";
txt_name.Text="";
txt_submit.Text="";
}
else
{
MessageBox.Show("注册失败!!!");
}
}
else
{
MessageBox.Show("密码输入不一致,请重新输入!!!");
}
}
该功能较为简单,只是对数据进行查取,但是也需要进行MD5密码验证,因为数据库存储的为用户MD5加密后的密码,如下为登录的核心代码:
public static string ToMD5(string source)
{
StringBuilder sb = new StringBuilder();
MD5 md5 = MD5.Create();
byte[] data = Encoding.UTF8.GetBytes(source);
data = md5.ComputeHash(data);
foreach (var item in data)
{
sb.Append(item.ToString("x2"));
}
return sb.ToString();
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入完整信息!!!");
return;
}
string count = textBox1.Text;
string pwd = ToMD5(textBox2.Text);
Models.Login user = new Models.Login { account = count,password=pwd };
bool result = ma.Login(user);
if (result)
{
this.Hide();
main mainForm = new main(textBox1.Text);
mainForm.StartPosition = FormStartPosition.CenterScreen;
mainForm.Show();
}
else
{
MessageBox.Show("账号或密码错误!!!");
}
}
catch
{
MessageBox.Show("登录失败!!!");
}
}
数据库操作
应为采用三层架构所以数据业务进行了分离
class DBHelper
{
public static string connString = @"Data Source=.;Initial Catalog=student1;Integrated Security=True";
//定义数据库连接对象
public static SqlConnection conn = new SqlConnection(connString);
//获取数据的方法,返回DataTable对象,参数为一个select语句
public static DataTable GetDataTable(string sqlStr)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dapt.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
//获取数据的重载方法,返回DataTable对象,参数为一个参数化的select语句和参数对象数组
public static DataTable GetDataTable(string sqlStr, SqlParameter[] param)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.Parameters.AddRange(param);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dapt.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
//执行更新的方法,返回一个布尔值,参数为一个insert|update|delete语句
public static bool ExcuteCommand(string sqlStr)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
conn.Close();
}
}
//执行更新的重载方法,返回一个布尔值,参数为一个参数化的insert|update|delete语句和参数对象数组
public static bool ExcuteCommand(string sqlStr, SqlParameter[] param)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.Parameters.AddRange(param);
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
conn.Close();
}
}
public static bool ExcuteCommand(List<String> sqlStr, List<SqlParameter[]> param)
{
int i = 0;
SqlCommand cmd = new SqlCommand();
using (TransactionScope ts = new TransactionScope())
{
cmd.Connection = conn;
conn.Open();
try
{
foreach (string item in sqlStr)
{
cmd.CommandType = CommandType.Text; //设置命令类型为SQL文本命令
cmd.CommandText = item; //设置要对数据源执行的SQL语句
cmd.Parameters.AddRange(param[i]);
i++;
cmd.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数
}
ts.Complete();
return true;
}
catch
{
return false;
}
finally
{
conn.Close();
sqlStr.Clear();
}
}
}
}
public class Login
{
public string account { get; set; }
public string password { get; set; }
public string name { get; set; }
}
public bool Register(Login user) //注册
{
string sqlStr = "insert into Login(账号,密码,昵称) values(@账号,@密码,@昵称)";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@账号",user.account),
new SqlParameter("@密码",user.password),
new SqlParameter("@昵称",user.name),
};
return DBHelper.ExcuteCommand(sqlStr, param);
}
public class LoginManager
{
LoginService service = new LoginService();
public bool Register(Login user)
{
return service.Register(user);
}
}
Models.Login user = new Models.Login { account = count, password = pwd, name = nc };
bool result = ma.Register(user);
if (result)
{
MessageBox.Show("注册成功!!!");
}
到此一个三层架构操作数据库添加就完成了
源码或者了解更多可以查看:winform之家