当前位置: 代码迷 >> .NET相关 >> 一个用三层架构实现数据访问的事例
  详细解决方案

一个用三层架构实现数据访问的事例

热度:195   发布时间:2016-04-24 02:48:49.0
一个用三层架构实现数据访问的例子

看了2天的三层架构,其实自己以前也看过这个,可以由于没有使用过,所以对于三层架构也只是知道罢了,昨天看了一下他一些内部的具体架构,三层分别是BLL、WEB、DAL,在web接受用户请求,bll处理业务,dal处理数据库事务,下面是一个简单的例子!这是一个添加新员工的页面:

后台代码如下:
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Text;
usingMaticsoft.Common;
usingLTP.Accounts.Bus;
usingSystem;
namespaceMaticsoft.Web.Employees
{
   publicpartialclassMYAdd:Page
   {
       protectedvoidPage_Load(objectsender,EventArgse)
        {
 
        }
       
 
       protectedvoidButtonOK_Click(objectsender,EventArgse)
       {
           stringERRM ="";
           if(!PageValidate.IsNumber(Temployeeid.Text.Trim()))
           {
               ERRM +="输入的id不是数字,不合法!";
            }
           if(this.Temployeeid.Text.Trim().Length ==0)
           {
               ERRM +="用户id不能为空!";
            }
           if(ERRM !="")
            {
               MessageBox.Show(this, ERRM);
                ERRM ="";
               return;
           }           intEmployeeID =int.Parse(this.Temployeeid.Text);           /
           stringEmployeesName =this.Temployeename.Text;
           Maticsoft.Model.Employeesmodel =newModel.Employees();                 //新建一个在dal层Employees的model对象,用于对数据库Employees表的数据处理
            model.EmployeeID = EmployeeID;                                                                 //将text里面用户输入的id数据传给model
            model.EmployeesName = EmployeesName;                                                    //将text里面用户输入的name数据传给model
           Maticsoft.BLL.Employeesbll =newBLL.Employees();                             //新建一个在bll层的employees对象,用于调用dal层的model对象
            bll.Add(model);                                                                                                   //在bll层调用add方法添加一条数据                                                                   
 
//////////////////////////////////////   bll层的add 方法    ///////////////////////////////////////////////////////////////////////////
                 /// 增加一条数据             
                                publicboolAdd(Maticsoft.Model.Employeesmodel)
                                {
                                                returndal.Add(model);                                          //调用dal层的add方法添加一条数据
                                }
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
 
 
//////////////////////////////////////  dal层的 add 方法    ///////////////////////////////////////////////////////////////////////////
            ///增加一数据
                                publicboolAdd(Maticsoft.Model.Employeesmodel)
                                {
                                                StringBuilderstrSql=newStringBuilder();
                                                strSql.Append("insert into Employees(");
                                                strSql.Append("EmployeeID,EmployeesName)");
                                                strSql.Append(" values (");
                                                strSql.Append("@EmployeeID,@EmployeesName)");
                                                SqlParameter[] parameters = {
                                                                                newSqlParameter("@EmployeeID",SqlDbType.Int,4),
                                                                                newSqlParameter("@EmployeesName",SqlDbType.VarChar,90)};
                                                parameters[0].Value = model.EmployeeID;
                                                parameters[1].Value = model.EmployeesName;
 
                                                introws=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
                                                if(rows >0)
                                                {
                                                                returntrue;
                                                }
                                                else
                                                {
                                                                returnfalse;
                                                }
                                }
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            Maticsoft.Common.MessageBox.ShowAndRedirect(this,"保存成功|!","MYAdd.aspx");                     //成功创建后弹出对话框,并跳转到当前页面!
 
        }
 
       protectedvoidButtonNo_Click(objectsender,EventArgse)
       {
           Response.Redirect("list.aspx");
        }
    
    }
}
  相关解决方案