文中的代码示例直接使用了博文http://www.cnblogs.com/qq731109249/archive/2013/01/16/2863201.html中的代码
三层架构由这些部分组成:
- 视图层(UI)
- 业务逻辑层(BLL)
- 数据库访问层(DAL)
- 实体(MODEL)
我们首先看一下,该架构各层之间的关系图
结合着图,我们来介绍一下各层
实体
将我们在开发过程中涉及到的对象,抽象成不同的类。
举例:
public class NewsModel
{
//新闻编号
private int nNewsId;
public int NNewsId
{
get { return nNewsId; }
set { nNewsId = value; }
}
//新闻名称
private string strNewsName;
public string StrNewsName
{
get { return strNewsName; }
set { strNewsName = value; }
}
}
数据访问层
该层代码的主要目的是实现对数据库的访问。
包括两类代码:
1、实现基本数据库增删改查操作的母类:由于对数据库的操作基本上不外乎增删改查,总不能涉及对一个表的操作,我们就重写一个增删改查方法吧,所以,统一写出一个增删改查类。
2、通过母类,实现各个具体数据库操作的具体类。
举个例子:
母类:
/// <summary>
///创建一个SqlHelper的数据库访问通用类,完成对数据库的所有操作
/// </summary>
public class SqlHelper
{
//定义数据库的连接字符串
private static readonly string connectionString = ConfigurationManager.ConnectionStrings["strConnectionString"].ConnectionString;
/// <summary>
/// 创建方法,完成对数据库的非查询的操作
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">传入的参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
string str = sql;
return cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// 完成查询的结果值
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">传入的参数数组</param>
/// <returns></returns>
public static int ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return Convert.ToInt32( cmd.ExecuteScalar());
}
}
}
/// <summary>
/// 主要执行查询操作
/// </summary>
/// <param name="sql">执行的sql语句</param>
/// <param name="parameters">参数数组</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
}
}
具体类:
/// <summary>
///NewsDALL 的摘要说明
/// </summary>
public class NewsDALL
{
//向数据库中插入新闻
public int AddNews(NewsModel model)
{
string sql = "insert into News (name,author,time,content,sort,isdelete) values(@name,@author,@time,@content,@sort,@isdelete)";
int nResult = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@name", model.StrNewsName), new SqlParameter("@author",model.StrNewsAuthor),new SqlParameter("@time", model.StrAddTime), new SqlParameter("@content", model.StrNewsContent), new SqlParameter("@sort", model.Sort), new SqlParameter("@isdelete", model.IsDelete1));
return nResult;
}
//执行数据库的删除操作
public int DeleteNew(int id)
{
string sql = "delete from News where id=@id";
int nResult = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@id", id));
return nResult;
}
//执行数据库的更新操作
public int UpdateNew(NewsModel model, int nID)
{
string sql = "update News set name=@name,time=@time,content=@content where id=" + nID;
int nResult = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@name", model.StrNewsName), new SqlParameter("@time", model.StrAddTime), new SqlParameter("@content", model.StrNewsContent));
return nResult;
}
//执行数据库的查询操作
public NewsModel GetNewsModel(int id)//声明一次从数据库中读取新闻的条数
{
string sql = "select * from News where id=@id";
DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@id", id));
if (dt.Rows.Count <= 0)
{
return null;
}
else if (dt.Rows.Count == 1)
{
NewsModel newModel = new NewsModel();
DataRow dr = dt.Rows[0];
newModel.StrNewsName = dr["name"].ToString();
newModel.StrNewsAuthor = dr["author"].ToString();
newModel.StrAddTime = dr["time"].ToString();
newModel.StrNewsContent = dr["content"].ToString();
newModel.Sort = (int)dr["sort"];
return newModel;
}
else
{
throw new Exception("出现异常!");
}
}
}
业务逻辑层
实现具体的业务逻辑,并调用数据访问层的类访问数据库、实例化实体类对象。
举个例子:
/// <summary>
///业务逻辑层主要处理视图层和数据库访问直接的关系
/// </summary>
public class NewsBLL
{//调用数据访问层的类访问数据库
//完成对数据库的添加
public static int AddNew(NewsModel model)
{
NewsDALL newDALL = new NewsDALL();
return newDALL.AddNews(model);
}
//完成对数据的删除
public static int DeleteNew(int i)
{
NewsDALL newDALL = new NewsDALL();
return newDALL.DeleteNew(i);
}
//实例化实体类对象并赋值
//返回一个新闻分类的对象
public static NewsModel GetModel(int intSort)
{
NewsModel model = new NewsModel();
if (intSort == 1)
{
model.StrNewSort1 = "学院新闻";
model.StrNewSort2 = "";
model.StrNewSort3 = "";
}
else if (intSort == 2)
{
model.StrNewSort1 = "公告通知";
model.StrNewSort2 = "";
model.StrNewSort3 = "";
}
..........
return model;
}
}
视图层
调用业务逻辑层的类来实现对应功能,同时获取页面值,赋予实例化的实体类。
示例:
public void InsertData()
{
NewsModel newModel = new NewsModel();
newModel.StrNewsName = this.TextBox1.Text;
newModel.StrNewsAuthor = this.TxtBoxAuthor.Text;
newModel.StrAddTime = this.TxtDate.Text;
newModel.StrNewsContent = Server.HtmlDecode(FCKeditor1.Value);
newModel.Sort =Convert.ToInt32( this.DropDownList2.SelectedValue.ToString());
//NewsBLL newBLL = new NewsBLL();
int nResult= NewsBLL.AddNew(newModel);
if (nResult != 0)
{
Response.Write("<script>alert('添加成功!')</script>");
}
else
{
Response.Write("<script>alert('添加失败!')</script>");
}
}