using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using NGuestBook.IDAL; using NGuestBook.Entity; namespace NGuestBook.AccessDAL { public class AdminDAL : IAdminDAL { /**//// <summary> /// 插入管理員 /// </summary> /// <param name="admin">管理員實體類</param> /// <returns>是否成功</returns> public bool Insert(AdminInfo admin) { string SQLCommand = "insert into [TAdmin]([Name],[Password]) values(@name,@password)"; OleDbParameter[] parameters ={ new OleDbParameter("name",admin.Name), new OleDbParameter("password",admin.Password) }; try { AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters); return true; } catch { return false; } } /**//// <summary> /// 刪除管理員 /// </summary> /// <param name="id">欲刪除的管理員的ID</param> /// <returns>是否成功</returns> public bool Delete(int id) { string SQLCommand = "delete from [TAdmin] where [ID]=@id"; OleDbParameter[] parameters ={ new OleDbParameter("id",id) }; try { AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters); return true; } catch { return false; } } /**//// <summary> /// 更新管理員信息 /// </summary> /// <param name="admin">管理員實體類</param> /// <returns>是否成功</returns> public bool Update(AdminInfo admin) { string SQLCommand = "update [TAdmin] set [Name]=@name,[Password]=@password where [ID]=@id"; OleDbParameter[] parameters ={ new OleDbParameter("id",admin.ID), new OleDbParameter("name",admin.Name), new OleDbParameter("password",admin.Password) }; try { AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters); return true; } catch { return false; } } /**//// <summary> /// 按ID取得管理員信息 /// </summary> /// <param name="id">管理員ID</param> /// <returns>管理員實體類</returns> public AdminInfo GetByID(int id) { string SQLCommand = "select * from [TAdmin] where [ID]=@id"; OleDbParameter[] parameters ={ new OleDbParameter("id",id) }; try { OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters); if (!dataReader.HasRows) { throw new Exception(); }
AdminInfo admin = new AdminInfo(); dataReader.Read(); admin.ID=(int)dataReader["ID"]; admin.Name=(string)dataReader["Name"]; admin.Password=(string)dataReader["Password"];
return admin; } catch { return null; } }
/**//// <summary> /// 按用戶名及密碼取得管理員信息 /// </summary> /// <param name="name">用戶名</param> /// <param name="password">密碼</param> /// <returns>管理員實體類,不存在時返回null</returns> public AdminInfo GetByNameAndPassword(string name, string password) { string SQLCommand = "select * from [TAdmin] where [Name]=@name and [Password]=@password"; OleDbParameter[] parameters ={ new OleDbParameter("name",name), new OleDbParameter("password",password), };
try { OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters); if (!dataReader.HasRows) { throw new Exception(); }
AdminInfo admin = new AdminInfo(); dataReader.Read(); admin.ID = (int)dataReader["ID"]; admin.Name = (string)dataReader["Name"]; admin.Password = (string)dataReader["Password"];
return admin; } catch { return null; } }
/**//// <summary> /// 按管理員名取得管理員信息 /// </summary> /// <param name="name">管理員名</param> /// <returns>管理員實體類</returns> public AdminInfo GetByName(string name) { string SQLCommand = "select * from [TAdmin] where [Name]=@name"; OleDbParameter[] parameters ={ new OleDbParameter("name",name), };
try { OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters); if (!dataReader.HasRows) { throw new Exception(); }
AdminInfo admin = new AdminInfo(); dataReader.Read(); admin.ID = (int)dataReader["ID"]; admin.Name = (string)dataReader["Name"]; admin.Password = (string)dataReader["Password"];
return admin; } catch { return null; } }
/**//// <summary> /// 取得全部管理員信息 /// </summary> /// <returns>管理員實體類集合</returns> public IList<AdminInfo> GetAll() { string SQLCommand = "select * from [TAdmin]"; try { OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, null); if (!dataReader.HasRows) { throw new Exception(); }
IList<AdminInfo> adminCollection = new List<AdminInfo>(); int i = 0; while (dataReader.Read()) { AdminInfo admin = new AdminInfo(); admin.ID = (int)dataReader["ID"]; admin.Name = (string)dataReader["Name"]; admin.Password = (string)dataReader["Password"];
adminCollection.Add(admin); i ; }
return adminCollection; } catch { return null; } } } }
|