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

2018-06-17 19:41:18来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

看了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");
        }
    
    }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET快速学习方案(.NET菜鸟的成长之路)

下一篇:.NET微信公众号开发-2.0创建自定义菜单