using System; using System.Collections.Generic; using System.Text; using NOVA.DevFx.Security.Model; using System.Data; using NOVA.DevFx.Data.Utils; using System.Data.SqlClient; namespace NOVA.DevFx.Security.DataHandler { /// /// 系统用户数据库访问类 /// public static class UserHandler { /// /// 将数据转换成实体类 /// public static User DataReaderToEntites(IDataReader dr) { User model= new User(); if (dr["id"].ToString() != "") { model.id = int.Parse(dr["id"].ToString()); } model.staff_id = dr["staff_id"].ToString(); model.name = dr["name"].ToString(); model.password = dr["password"].ToString(); if (dr["super_flag"].ToString() != "") { model.super_flag = bool.Parse(dr["super_flag"].ToString()); } model.last_login_ip = dr["last_login_ip"].ToString(); if (dr["last_login_time"].ToString() != "") { model.last_login_time = DateTime.Parse(dr["last_login_time"].ToString()); } model.description = dr["description"].ToString(); if (dr["sort_index"].ToString() != "") { model.sort_index = int.Parse(dr["sort_index"].ToString()); } return model; } /// /// 获得数据列表 /// public static List GetList(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index "); strSql.Append(" FROM s_user "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString()); List m_lst = new List(); while (read.Read()) { User item = DataReaderToEntites(read); if (item != null) { m_lst.Add(item); } } read.Close(); return m_lst; } /// /// 根据角色编号获取所有用户 /// /// public static List GetUserListByRoleID(int roleid) { return GetList(" [id] in(select user_id from s_user_in_role where role_id=" + roleid.ToString() + ")"); } /// /// 获取指定编号的用户信息 /// /// /// public static User GetItem(int uid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index "); strSql.Append(" FROM s_user "); strSql.Append(" WHERE [id]=@id "); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; int n = 0; parameters[n++].Value = uid; SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString(), parameters); User item=null; if (read.Read()) { item = DataReaderToEntites(read); } if (item != null) { read.Close(); return item; } else { return null; } } /// /// 获取指定用户名的用户信息 /// /// /// public static User GetItemByName(string uname) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index "); strSql.Append(" FROM s_user "); strSql.Append(" WHERE [name]=@name "); SqlParameter[] parameters = { new SqlParameter("@name", SqlDbType.VarChar,20)}; int n = 0; parameters[n++].Value = uname; SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString(), parameters); User item = null; if (read.Read()) { item = DataReaderToEntites(read); } if (item != null) { read.Close(); return item; } else { return null; } } } }