public class AdminService { #region 根据登录名和密码查询管理员 /// <summary> /// 根据登录名和密码查询管理员 /// </summary> /// <param name="admin">用户对象</param> /// <returns>用户对象</returns> public Admin GetAdminByParas(Admin admin) { string sql = "SELECT * FROM Admin WHERE LoginId=@LoginId and LoginPwd=@LoginPwd"; SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LoginId",admin.LoginId), new SqlParameter("@LoginPwd",admin.LoginPwd) }; List<Admin> adminList = GetAdminBySql(CommandType.Text, sql, paras); if (adminList.Count > 0) return adminList[0]; else return admin; } #endregion public int DeleteAdminById(int id) { string sql = "DELETE FROM Admin WHERE Id=@Id"; SqlParameter para = new SqlParameter("@Id", id); int rowCount = Convert.ToInt32(DBHelper.ExecuteNonQuery(CommandType.Text, sql, para)); return rowCount; } public List<Admin> GetAllAdmin() { string sql = "SELECT * FROM Admin"; return GetAdminBySql(CommandType.Text, sql, null); } public int UpdateAdmin(Admin admin) { string sql = "UPDATE Admin SET LoginId=@LoginId,Name=@Name,Phone=@Phone WHERE Id=@Id"; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@Id", admin.Id), new SqlParameter("@LoginId", admin.LoginId), new SqlParameter("@Name", admin.Name), new SqlParameter("@Phone", admin.Phone) }; int rowCount = Convert.ToInt32(DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras)); return rowCount; } public List<Admin> GetAdminByNameAndPhone(string name, string phone) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@name",name), new SqlParameter("@phone",phone) }; return GetAdminBySql(CommandType.StoredProcedure, "proc_GetAdminByNamePhone", pars); } public Admin GetAdmin(int id) { string sql = "SELECT * FROM Admin WHERE Id=@Id"; SqlParameter paras = new SqlParameter("@Id", id); List<Admin> list = GetAdminBySql(CommandType.Text, sql, paras); if (list.Count > 0) { return list[0]; } return new Admin(); } #region 分页 public List<Admin> GetAdminPages(int pageSize,int pageIndex) { SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@PageSize",pageSize), new SqlParameter("@PageIndex",pageIndex) }; return GetAdminBySql(CommandType.StoredProcedure,"proc_AdminPages",pars); } //获取admin表共有多少页 public int GetAdminPageNums(int pageSize) { int numbers = Convert.ToInt32( DBHelper.ExecuteScalar(CommandType.Text, "Select count(*) from admin", null)); return Convert.ToInt32( Math.Ceiling((numbers / Convert.ToDecimal(pageSize)))); } public List<Admin> GetAdminPagesNew(int pageSize,int pageIndex,string where) { SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@pageSize",pageSize), new SqlParameter("@pageIndex",pageIndex), new SqlParameter("@where",where) }; return GetAdminBySql(CommandType.StoredProcedure, "proc_AdminPagesNew", pars); } #endregion #region 基础方法 //查询语句中字段为 * 时的通用方法。 private List<Admin> GetAdminBySql(CommandType commandType, string commandText, params SqlParameter[] paras) { List<Admin> list = new List<Admin>(); using (SqlDataReader reader = DBHelper.ExecuteGetReader(commandType, commandText, paras)) { while (reader.Read()) { Admin admin = new Admin { Id = (int)reader["Id"], LoginId = reader["LoginId"].ToString(), LoginPwd = reader["LoginPwd"].ToString(), Name = reader["Name"].ToString(), Phone = reader["Phone"].ToString(), LastLoginDate = (DateTime)reader["LastLoginDate"] }; list.Add(admin); } } return list; } #endregion }