數(shù)據(jù)訪問層的第一種實現(xiàn):Access SQL(2)_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:依賴注入機制及IoC的設計與實現(xiàn)
      我們設計的分層架構(gòu),層與層之間應該是松散耦合的。因為是單向單一調(diào)用,所以,這里的“松散耦合”實際是指上層類不能具體依賴于下層類,而應該依賴于下層提供的一個接口。這樣,上層

      2.實現(xiàn)具體的數(shù)據(jù)訪問操作類

      因為前面已經(jīng)定義了數(shù)據(jù)訪問層接口,所以實現(xiàn)數(shù)據(jù)訪問操作類就是很機械的工作了。下面僅以Admin的數(shù)據(jù)訪問操作類為例:

      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;
      }
      }
      }
      }

      可以看到,這里主要包括三種類型的操作,一種是修改型,如Insert;一種是返回單個實體類型,如GetByID;還有一種是返回實體類集合型,如GetAll。

      MessageDAL和CommentDAL的實現(xiàn)非常相似,在這里不再贅述。

      分享:近期的幾個ASP.NET開發(fā)經(jīng)驗總結(jié)和收集
      一:頁面中Page_Load事件 和 Page.IsPostBack執(zhí)行兩次的原因. 原因一: 當<%@Page....中沒有AutoEventWireup定義時會導致Page_Load執(zhí)行兩次,如果有定義,且值為true時,在IsPostBack中動態(tài)

      共2頁上一頁12下一頁
      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關.Net教程