三层架构的基本例子
具体的概念就不说了。主要是让理解了三层架构的当做参考
一、MDL
namespace MDL
{
public class Product
{
#region 变量
private int _Product_ID = 0;
//-------------------------------------
private string _Product_Name = "";
#endregion
#region 属性
public int Product_ID
{
get { return _Product_ID; }
set { _Product_ID = value; }
}
//-------------------------------------
public string Product_Name
{
get { return _Product_Name; }
set { _Product_Name = value; }
}
#endregion
}
二、DAL
主要分为 获取(主要用来取值)、列表(主要用来查询)、修改和增加以及删除操作
用到的命名空间有 :
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Collections; using DBUtility;
1>下面就开始获取,返回的是一个datatable类型的表
#region 获取
public DataTable Get(string strProduct_ID)
{
string strSql = @" select *
from T_Product
where Product_ID = @Product_ID
";
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@Product_ID", strProduct_ID)
};
return SQLHelper.Query(strSql, parameters);
}
#endregion2>列表函数的参数为一个哈希表(可能有的人不大习惯),不过看个人习惯了,用久了也会慢慢的习惯。
public DataTable List(Hashtable htParam)
{
string strSql = @"select Pro.*
where (@Product_Name=') or (Product_Name like '%'+@Product_Name+'%')) order by Pro.Product_Order asc
";
//本实例还涉及到一个sql语句的巧妙使用,一个逻辑的“or”
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@Product_Name", Basis.GetHashVal(htParam,"Product_Name")) };
return SQLHelper.Query(strSql, parameters);
}
3>删除部分就不说了,和获取差不多,只不过是返回值不一样。
4>更新就更新的给平时不一样了,因为更新的参数是一个MDL----一个整体
public int Update(MDL.Product mdlObj) { string strSql = @"update T_Product set Product_Name = @Product_Name where Product_ID = @Product_ID"; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Product_Name", mdlObj.Product_Name), new SqlParameter("@Product_ID", mdlObj.Product_ID) }; return SQLHelper.ExecuteNonQuery(strSql, parameters);}
三、BLL
主要处理UI层的事务。主要也是分为获取,查询,更改、添加和删除。
1>还是先从或许说起,获取返回的结果是个MDL,这样就能得到所有MDL中的每个字段,然后在UI中可以灵活的运用自如。
#region 获取 public MDL.Product Get(string strProduct_ID) { MDL.Product mdlObj = new MDL.Product(); DataTable dt = DAL.Get(strProduct_ID); if (dt.Rows.Count > 0) { mdlObj.Product_ID = Convert.ToInt32(dt.Rows[0]["Product_ID"]); mdlObj.Product_Name = dt.Rows[0]["Product_Name"].ToString();} return mdlObj; } #endregion
2>查询 其参数为一个哈希表,这个函数在DAL和UI层写的比较多,所有在BLL层就可以省点力气了#region 列表 public DataTable List(Hashtable htParam) { return DAL.List(htParam); } #endregion3>删除、添加、更改和查询差不多,只是在添加和更改的参数是MDL型的,在此就不啰嗦了四、UI层 本层是比较灵活,所以只用一个绑定下拉框为例说明,其他的慢慢去钻研private static readonly BLL.Product_Type bllSec = new BLL.Product_Type();protected void BindProType() { ddlProType_ID.DataSource = bllSec.GetAllModule(strLangVer); ddlProType_ID.DataTextField = "RootPath"; ddlProType_ID.DataValueField = "ProType_ID"; ddlProType_ID.DataBind(); ddlProType_ID.Items.Insert(0, new ListItem("请选择...", "")); }
浙公网安备 33010602011771号