/// <summary> /// 根据主键查找对象 /// </summary> /// <param name="userId"></param> /// <returns></returns> public static User GetUserById(string userId) { string sql = "select * from viewBranchDepartUsers where userId=@uId"; SqlParameter[] parameters = { new SqlParameter("@uId", userId) }; SqlDataReader reader = DBHelper.GetReader(sql, parameters); if (reader.Read()) { User user = new User(); user.UserId = Convert.ToString(reader["UserId"]); user.UserName = Convert.ToString(reader["UserName"]); user.Password = Convert.ToString(reader["Password"]); user.DepartId = Convert.ToInt32(reader["DepartId"]); user.DepartName = Convert.ToString(reader["DepartName"]); user.BranchId = Convert.ToInt32(reader["BranchId"]); user.BranchName = Convert.ToString(reader["BranchName"]); user.Gender = Convert.ToInt32(reader["Gender"]); //外键对象 int roleId = Convert.ToInt32(reader["RoleId"]); int userStateId = Convert.ToInt32(reader["userStateId"]); reader.Close(); user.Role = RoleService.GetRoleById(roleId); user.UserState = UserStateService.GetUserStateById(userStateId); return(user); } else { reader.Close(); return(null); } //return GetUserBySql(sql, parameters)[0]; }
private static IList <User> GetUserBySql(string sql, params SqlParameter[] parameters) { IList <User> lists = new List <User>(); try { DataTable table = DBHelper.GetDataSet(sql, parameters); foreach (DataRow row in table.Rows) { User user = new User(); user.UserId = Convert.ToString(row["UserId"]); user.UserName = Convert.ToString(row["UserName"]); user.Password = Convert.ToString(row["Password"]); user.DepartId = Convert.ToInt32(row["DepartId"]); user.DepartName = Convert.ToString(row["DepartName"]); user.BranchId = Convert.ToInt32(row["BranchId"]); user.BranchName = Convert.ToString(row["BranchName"]); user.Gender = Convert.ToInt32(row["Gender"]); //外键对象 int roleId = Convert.ToInt32(row["RoleId"]); user.Role = RoleService.GetRoleById(roleId); int userStateId = Convert.ToInt32(row["userStateId"]); user.UserState = UserStateService.GetUserStateById(userStateId); lists.Add(user); } return(lists); } catch (Exception ex) { throw ex; } }