国产毛片午夜福利,国产黄网,国产亚洲天堂,97国产精品

  •  
    數(shù)據(jù)訪問層的第一種實現(xiàn):Access+SQL
    發(fā)布時間:2008-06-27   瀏覽次數(shù):1181440
    數(shù)據(jù)訪問層的種實現(xiàn):Access+SQL 經(jīng)過上面篇文章的介紹,整個系統(tǒng)的框架算是基本搭建完了,下面,我們要具體實現(xiàn)各個層次。關于數(shù)據(jù)訪問層的實現(xiàn),我準備討論三種實現(xiàn)方式,這一篇文章討論種:Access+動態(tài)生成SQL。 顧名思義,這種實現(xiàn)將使用Access作為后臺數(shù)據(jù)庫,而操作方式也是最基本的使用SQL命令。 在具體編寫實現(xiàn)代碼之前,我們需要做一些準備工作: 步,我們要將Access數(shù)據(jù)庫搭建完成,具體做法如下。 在Web工程下新建一個文件夾,命名為AccessData,并在其中新建一個mdb文件(即Access數(shù)據(jù)庫文件),按照前面介紹過的數(shù)據(jù)庫設計構架,將數(shù)據(jù)表及表間關系建好,這里不再贅述。 第二步,我們要進行一些配置。 打開Web工程下的Web.config文件,在其中的appSettings節(jié)點下,添加如下鍵值: 條為Access的連接字符串,第二條為Access數(shù)據(jù)庫文件的路徑,其中“~”表示網(wǎng)站根目錄。 第三步,新建一個工程。 我們要新建一個工程AccessDAL,用來存放Access數(shù)據(jù)訪問層的代碼。 準備工作做完了,現(xiàn)在來實現(xiàn)具體的代碼。 1.編寫數(shù)據(jù)訪問助手類 因為很多數(shù)據(jù)訪問操作流程很相似,所以,這里將一些可復用的代碼抽取出來,編寫成助手類,以此減少代碼量,提高代碼復用性。 這個助手類放在AccessDAL下,叫AccessDALHelper,主要負責Access數(shù)據(jù)庫的訪問。它包括三個方法: GetConnectionString:從配置文件中讀取配置項,組合成連接字符串。 ExecuteSQLNonQuery:執(zhí)行指定SQL語句,不返回任何值,一般用于Insert,Delete,Update命令。 ExecuteSQLDataReader:執(zhí)行SQL語句返回查詢結(jié)果,一般用于Select命令。 具體代碼如下: using System;using System.Web;using System.Web.Caching;using System.Configuration;using System.Data;using System.Data.OleDb;using NGuestBook.Utility;namespace NGuestBook.AccessDAL{ /**//// /// Access數(shù)據(jù)庫操作助手 /// public sealed class AccessDALHelper { /**//// /// 讀取Access數(shù)據(jù)庫的連接字符串 /// 首先從緩存里讀取,如果不存在則到配置文件中讀取,并放入緩存 /// /// Access數(shù)據(jù)庫的連接字符串 private static string GetConnectionString() { if (CacheAccess.GetFromCache("AccessConnectionString") != null) { return CacheAccess.GetFromCache("AccessConnectionString").ToString(); } else { string dbPath = ConfigurationManager.AppSettings["AccessPath"]; string dbAbsolutePath = HttpContext.Current.Server.MapPath(dbPath); string connectionString = ConfigurationManager.AppSettings["AccessConnectionString"]; CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config")); CacheAccess.SaveToCache("AccessConnectionString", connectionString.Replace("{DBPath}", dbAbsolutePath), fileDependency); return connectionString.Replace("{DBPath}", dbAbsolutePath); } } /**//// /// 執(zhí)行SQL語句并且不返回任何值 /// /// 所執(zhí)行的SQL命令 /// 參數(shù)集合 public static void ExecuteSQLNonQuery(string SQLCommand,OleDbParameter[] parameters) { OleDbConnection connection = new OleDbConnection(GetConnectionString()); OleDbCommand command = new OleDbCommand(SQLCommand, connection); for (int i = 0; i < parameters.Length; i++) { command.Parameters.Add(parameters[i]); } connection.Open(); command.ExecuteNonQuery(); connection.Close(); } /**//// /// 執(zhí)行SQL語句并返回包含查詢結(jié)果的DataReader /// /// 所執(zhí)行的SQL命令 /// 參數(shù)集合 /// public static OleDbDataReader ExecuteSQLDataReader(string SQLCommand,OleDbParameter[] parameters) { OleDbConnection connection = new OleDbConnection(GetConnectionString()); OleDbCommand command = new OleDbCommand(SQLCommand, connection); for (int i = 0; i < parameters.Length; i++) { command.Parameters.Add(parameters[i]); } connection.Open(); OleDbDataReader dataReader = command.ExecuteReader(); //connection.Close(); return dataReader; } }} 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 { /**//// /// 插入管理員 /// /// 管理員實體類 /// 是否成功 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; } } /**//// /// 刪除管理員 /// /// 欲刪除的管理員的ID /// 是否成功 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; } } /**//// /// 更新管理員信息 /// /// 管理員實體類 /// 是否成功 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; } } /**//// /// 按ID取得管理員信息 /// /// 管理員ID /// 管理員實體類 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; } } /**//// /// 按用戶名及密碼取得管理員信息 /// /// 用戶名 /// 密碼 /// 管理員實體類,不存在時返回null 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; } } /**//// /// 按管理員名取得管理員信息 /// /// 管理員名 /// 管理員實體類 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; } } /**//// /// 取得全部管理員信息 /// /// 管理員實體類集合 public IList GetAll() { string SQLCommand = "select * from [TAdmin]"; try { OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, null); if (!dataReader.HasRows) { throw new Exception(); } IList adminCollection = new List(); 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)非常相似,在這里不再贅述。
    立即預約