asp.net中的3层架构 [英] 3tier architecture in asp.net

查看:85
本文介绍了asp.net中的3层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建任何应用程序时,只是将所有代码写入单个文件中



例如:

创建登录页面asp.net with Sql Server200



如何实现3>



给我简单理解

解决方案

首先创建BEL.CS(业务实体层)



BEL.CS



  private   string  _UserName; 
private string _Password;

public string UserName
{
get
{ return _UserName;
}
set
{
_UserName = value ;
}
}

public string 密码
{
get
{
return _Password;
}
set
{
_Password = value ;
}
}







DAL.CS的第二个(数据访问)图层)



  //   SQL连接字符串 
字符串 ConnectionString = ConfigurationManager.AppSettings [ < span class =code-string> LocalConnection]。ToString();


public string UserInformation(BEL objBELUserDetails)
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand( sp_userinformation ,con);
cmd.CommandType = CommandType.StoredProcedure;
尝试
{
cmd.Parameters.AddWithValue( < span class =code-string> @ UserName,objBELUserDetails.UserName);
cmd.Parameters.AddWithValue( @ Password,objBELUserDetails.Password);
cmd.Parameters.Add( @ ERROR,SqlDbType.Char, 500 );
cmd.Parameters [ @ ERROR]。Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string strMessage =( string )cmd.Parameters [ @ ERROR]。价值;
con.Close();
return strMessage;
}
catch (例外情况)
{
throw ex;
}
最后
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}







BLL.CSS的第三个(商务访问)图层)



BLL.CSS

  public   string  UserDetails(BEL objUserDetails)
{
DAL objUserDAL = new DAL ();
尝试
{
return objUserDAL.UserInformation(objUserDetails);
}
catch (例外情况)
{
throw ex;
}
最后
{
objUserDAL = null ;
}
}





现在在演示层中



我的Login.aspx.cs



<前lang =c#> 受保护 void btnlogin_Click( object sender,EventArgs e)
{
尝试
{
字符串输出= 字符串 .Empty;
BEL objUserBEL = new BEL();

objUserBEL.UserName = txtuser.Text;
objUserBEL.Password = txtpwd.Text;
BLL objUserBLL = new BLL();
输出= objUserBLL.InsertUserDetails(objUserBEL);
Response.Redirect( Home.aspx);
}
catch (例外情况)
{

}
}


这是关于如何创建3层架构的教程:使用N层体系结构创建ASP.NET应用程序 [ ^ ]



和这是一个相同的示例站点: YaBlogEngine - A用ASP.NET / C#编写的微博客引擎 [ ^ ]


when am creating any application just am writing all the code in a single file

For example:
Am creating login page in asp.net with Sql Server2008

how to implement in 3tire

give me Simple Understanding

解决方案

First Create BEL.CS (Business Entity Layer)

BEL.CS

private string _UserName;
private string _Password;

public string UserName
{
get
{return _UserName;
}
set
{
_UserName = value;
}
}

public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}




Second One For DAL.CS (Data Access Layer)

//SQL Connection string 
string ConnectionString = ConfigurationManager.AppSettings["LocalConnection"].ToString();


public string UserInformation(BEL objBELUserDetails)
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("sp_userinformation", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@UserName",objBELUserDetails.UserName);
cmd.Parameters.AddWithValue("@Password", objBELUserDetails.Password);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string strMessage = (string) cmd.Parameters["@ERROR"].Value;
con.Close();
return strMessage;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}




Third One For BLL.CSS (Business Access Layer)

BLL.CSS

public string UserDetails(BEL objUserDetails)
{
DAL objUserDAL = new DAL();
try
{
return objUserDAL.UserInformation(objUserDetails);
}
catch (Exception ex)
{
throw ex;
}
finally
{
objUserDAL = null;
}
}



Now In Presentation Layer

My Login.aspx.cs

protected void btnlogin_Click(object sender, EventArgs e)
{
try
{
string Output = string.Empty;
BEL objUserBEL = new BEL();

objUserBEL.UserName = txtuser.Text;
objUserBEL.Password = txtpwd.Text;
BLL objUserBLL = new BLL();
Output = objUserBLL.InsertUserDetails(objUserBEL);
Response.Redirect("Home.aspx");
}
catch(exception ex)
{

}
} 


Here is the tutorial on how to create a 3-tier architecture: Creating ASP.NET Applications with N-Tier Architecture[^]

and here is one example site for the same: YaBlogEngine - A Tiny Blog Engine written in ASP.NET/C#[^]


这篇关于asp.net中的3层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆