数据库|mysql教程
基于,.NET,平台,分层,架构,实战,数据,访问,一种,实
数据库-mysql教程
易语言独立团模块源码,crane vscode,ubuntu常用软件安装,tomcat jedis,sqlite 导入 文本,搭建网站不用服务器吗,移动端选择省份插件,前端fish框架教学,nutch网络爬虫,php秒杀 实现过程,成都seo教程,问答网站模板,html买花网页源代码,百度网页模板,手机页面点击图片放大,php文件上传管理系统,vb开机启动程序lzw
经过上面篇文章的介绍,整个系统的 框架 算是基本搭建完了,下面,我们要具体 实现 各个层次。关于 数据 访问 层的 实现 ,我准备讨论三种 实现 方式,这一篇文章讨论 第一 种:Access+动态生成SQL。 顾名思义,这种 实现 将使用Access作为后台 数据 库,而
c 远程桌面控制源码,ubuntu换源18.04,反爬虫系统教学,php gmtdate,北京营销seolzw
mybatis源码官网下载,ubuntu16.04lv,tomcat怎么看war,kali搭建爬虫,新浪微博面试php,seo排名公司丿乐云seolzw
经过上面篇文章的介绍,整个系统的框架算是基本搭建完了,下面,我们要具体实现各个层次。关于数据访问层的实现,我准备讨论三种实现方式,这一篇文章讨论第一种:Access+动态生成SQL。
顾名思义,这种实现将使用Access作为后台数据库,而操作方式也是最基本的使用SQL命令。
在具体编写实现代码之前,我们需要做一些准备工作:
第一步,我们要将Access数据库搭建完成,具体做法如下。
在Web工程下新建一个文件夹,命名为AccessData,并在其中新建一个mdb文件(即Access数据库文件),按照前面介绍过的数据库设计构架,将数据表及表间关系建好,这里不再赘述。
第二步,我们要进行一些配置。
打开Web工程下的Web.config文件,在其中的appSettings节点下,添加如下键值:
第一条为Access的连接字符串,第二条为Access数据库文件的路径,其中“~”表示网站根目录。
第三步,新建一个工程。
我们要新建一个工程AccessDAL,用来存放Access数据访问层的代码。
准备工作做完了,现在来实现具体的代码。
1.编写数据访问助手类
因为很多数据访问操作流程很相似,所以,这里将一些可复用的代码抽取出来,编写成助手类,以此减少代码量,提高代码复用性。
这个助手类放在AccessDAL下,叫AccessDALHelper,主要负责Access数据库的访问。它包括三个方法:
GetConnectionString:从配置文件中读取配置项,组合成连接字符串。
ExecuteSQLNonQuery:执行指定SQL语句,不返回任何值,一般用于Insert,Delete,Update命令。
ExecuteSQLDataReader:执行SQL语句返回查询结果,一般用于Select命令。
具体代码如下:
AccessDALHelper.cs:
AccessDALHelper
1using System;
2using System.Web;
3using System.Web.Caching;
4using System.Configuration;
5using System.Data;
6using System.Data.OleDb;
7using NGuestBook.Utility;
8
9namespace NGuestBook.AccessDAL
10{
11 /**////
14 public sealed class AccessDALHelper
15 {
16 /**////
20 /// Access数据库的连接字符串
21 private static string GetConnectionString()
22 {
23 if (CacheAccess.GetFromCache(“AccessConnectionString”) != null)
24 {
25return CacheAccess.GetFromCache(“AccessConnectionString”).ToString();
26 }
27 else
28 {
29string dbPath = ConfigurationManager.AppSettings[“AccessPath”];
30string dbAbsolutePath = HttpContext.Current.Server.MapPath(dbPath);
31string connectionString = ConfigurationManager.AppSettings[“AccessConnectionString”];
32
33CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath(“Web.Config”));
34CacheAccess.SaveToCache(“AccessConnectionString”, connectionString.Replace(“{DBPath}”, dbAbsolutePath), fileDependency);
35
36return connectionString.Replace(“{DBPath}”, dbAbsolutePath);
37 }
38 }
39
40 /**////
43 /// 所执行的SQL命令44 /// 参数集合45 public static void ExecuteSQLNonQuery(string SQLCommand,OleDbParameter[] parameters)
46 {
47 OleDbConnection connection = new OleDbConnection(GetConnectionString());
48 OleDbCommand command = new OleDbCommand(SQLCommand, connection);
49
50 for (int i = 0; i < parameters.Length; i++)51 {52command.Parameters.Add(parameters);53 }5455 connection.Open();56 command.ExecuteNonQuery();57 connection.Close();58 }5960 /**////
63 /// 所执行的SQL命令64 /// 参数集合65 ///
66 public static OleDbDataReader ExecuteSQLDataReader(string SQLCommand,OleDbParameter[] parameters)
67 {
68 OleDbConnection connection = new OleDbConnection(GetConnectionString());
69 OleDbCommand command = new OleDbCommand(SQLCommand, connection);
70
71 for (int i = 0; i < parameters.Length; i++)72 {73command.Parameters.Add(parameters);74 }7576 connection.Open();77 OleDbDataReader dataReader = command.ExecuteReader();78 //connection.Close();7980 return dataReader;81 }82 }83}
复制代码
2.实现具体的数据访问操作类
因为前面已经定义了数据访问层接口,所以实现数据访问操作类就是很机械的工作了。下面仅以Admin的数据访问操作类为例:
AdminDAL:
AdminDAL
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.OleDb;
6using NGuestBook.IDAL;
7using NGuestBook.Entity;
8
9namespace NGuestBook.AccessDAL
10{
11 public class AdminDAL : IAdminDAL
12 {
13 /**////
16 /// 管理员实体类17 /// 是否成功
18 public bool Insert(AdminInfo admin)
19 {
20 string SQLCommand = “insert into [TAdmin]([Name],[Password]) values(@name,@password)”;
21 OleDbParameter[] parameters ={
22new OleDbParameter(“name”,admin.Name),
23new OleDbParameter(“password”,admin.Password)
24 };
25
26 try
27 {
28AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters);
29return true;
30 }
31 catch
32 {
33return false;
34 }
35 }
36
37 /**////
40 /// 欲删除的管理员的ID41 /// 是否成功
42 public bool Delete(int id)
43 {
44 string SQLCommand = “delete from [TAdmin] where [ID]=@id”;
45 OleDbParameter[] parameters ={
46new OleDbParameter(“id”,id)
47 };
48
49 try
50 {
51AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters);
52return true;
53 }
54 catch
55 {
56return false;
57 }
58 }
59
60 /**////
63 /// 管理员实体类64 /// 是否成功
65 public bool Update(AdminInfo admin)
66 {
67 string SQLCommand = “update [TAdmin] set [Name]=@name,[Password]=@password where [ID]=@id”;
68 OleDbParameter[] parameters ={
69new OleDbParameter(“id”,admin.ID),
70new OleDbParameter(“name”,admin.Name),
71new OleDbParameter(“password”,admin.Password)
72 };
73
74 try
75 {
76AccessDALHelper.ExecuteSQLNonQuery(SQLCommand, parameters);
77return true;
78 }
79 catch
80 {
81return false;
82 }
83 }
84
85 /**////
88 /// 管理员ID89 /// 管理员实体类
90 public AdminInfo GetByID(int id)
91 {
92 string SQLCommand = “select * from [TAdmin] where [ID]=@id”;
93 OleDbParameter[] parameters ={
94new OleDbParameter(“id”,id)
95 };
96
97 try
98 {
99OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters);
100if (!dataReader.HasRows)
101{
102 throw new Exception();
103}
104
105AdminInfo admin = new AdminInfo();
106dataReader.Read();
107admin.ID=(int)dataReader[“ID”];
108admin.Name=(string)dataReader[“Name”];
109admin.Password=(string)dataReader[“Password”];
110
111return admin;
112 }
113 catch
114 {
115return null;
116 }
117 }
118
119 /**////
122 /// 用户名123 /// 密码124 /// 管理员实体类,不存在时返回null
125 public AdminInfo GetByNameAndPassword(string name, string password)
126 {
127 string SQLCommand = “select * from [TAdmin] where [Name]=@name and [Password]=@password”;
128 OleDbParameter[] parameters ={
129new OleDbParameter(“name”,name),
130new OleDbParameter(“password”,password),
131 };
132
133 try
134 {
135OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters);
136if (!dataReader.HasRows)
137{
138 throw new Exception();
139}
140
141AdminInfo admin = new AdminInfo();
142dataReader.Read();
143admin.ID = (int)dataReader[“ID”];
144admin.Name = (string)dataReader[“Name”];
145admin.Password = (string)dataReader[“Password”];
146
147return admin;
148 }
149 catch
150 {
151return null;
152 }
153 }
154
155 /**////
158 /// 管理员名159 /// 管理员实体类
160 public AdminInfo GetByName(string name)
161 {
162 string SQLCommand = “select * from [TAdmin] where [Name]=@name”;
163 OleDbParameter[] parameters ={
164new OleDbParameter(“name”,name),
165 };
166
167 try
168 {
169OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, parameters);
170if (!dataReader.HasRows)
171{
172 throw new Exception();
173}
174
175AdminInfo admin = new AdminInfo();
176dataReader.Read();
177admin.ID = (int)dataReader[“ID”];
178admin.Name = (string)dataReader[“Name”];
179admin.Password = (string)dataReader[“Password”];
180
181return admin;
182 }
183 catch
184 {
185return null;
186 }
187 }
188
189 /**////
192 /// 管理员实体类集合
193 public IList GetAll()
194 {
195 string SQLCommand = “select * from [TAdmin]”;
196 try
197 {
198OleDbDataReader dataReader = AccessDALHelper.ExecuteSQLDataReader(SQLCommand, null);
199if (!dataReader.HasRows)
200{
201 throw new Exception();
202}
203
204IList adminCollection = new List();
205int i = 0;
206while (dataReader.Read())
207{
208 AdminInfo admin = new AdminInfo();
209 admin.ID = (int)dataReader[“ID”];
210 admin.Name = (string)dataReader[“Name”];
211 admin.Password = (string)dataReader[“Password”];
212
213 adminCollection.Add(admin);
214 i++;
215}
216
217return adminCollection;
218 }
219 catch
220 {
221return null;
222 }
223 }
224 }
225}
复制代码
可以看到,这里主要包括三种类型的操作,一种是修改型,如Insert;一种是返回单个实体类型,如GetByID;还有一种是返回实体类集合型,如GetAll。
MessageDAL和CommentDAL的实现非常相似,在这里不再赘述。