| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5093 人关注过本帖
标题:求ASP.NET三层结构
只看楼主 加入收藏
caiyakang
Rank: 2
等 级:新手上路
威 望:5
帖 子:2111
专家分:0
注 册:2005-3-24
收藏
 问题点数:0 回复次数:36 
求ASP.NET三层结构

求ASP.NET三层结构说明

搜索更多相关主题的帖子: NET ASP 结构 
2006-05-11 01:06
小笨笨
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:1169
专家分:0
注 册:2006-4-17
收藏
得分:0 

以下是转的:

典型的三层结构范例源码(数据层、商务层、表示层)
数据层:
数据层:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace YiPu.WebModules.Accounts.Data
{
/// <summary>
/// User 的摘要说明。
/// </summary>
public class User : YiPu.WebModules.Data.DbObject
{
public User(string newConnectionString): base(newConnectionString)
{ }

public bool Create(
string userID,
byte[] password,
string userName,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[]
parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 12),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit)
};

parameters[0].Value = userID;
parameters[1].Value = password;
parameters[2].Value = userName;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[10].Value = mobile;
parameters[11].Value = registerTime;
parameters[12].Value = invalidTime;
parameters[13].Value = question;
parameters[14].Value = answer;
parameters[15].Value = country;
parameters[16].Value = province;
parameters[17].Value = city;
parameters[18].Value = code;
parameters[19].Value = address;
parameters[20].Value = isRejected;

RunProcedure("sp_Accounts_CreateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}

public DataRow Retrieve(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;

using (DataSet users = RunProcedure( "sp_Accounts_GetUserDetails", parameters, "Users" ))
{
return users.Tables[0].Rows[0];
}
}

public bool Update(
string userID,
string userName,
byte[] password,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 20),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit),
};

parameters[0].Value = userID;
parameters[1].Value = userName;
parameters[2].Value = password;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[11].Value = mobile;
parameters[12].Value = registerTime;
parameters[13].Value = invalidTime;
parameters[14].Value = question;
parameters[15].Value = answer;
parameters[16].Value = country;
parameters[17].Value = province;
parameters[18].Value = city;
parameters[19].Value = code;
parameters[20].Value = address;
parameters[21].Value = isRejected;

RunProcedure("sp_Accounts_UpdateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}

public bool Delete(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
int rowsAffected;

parameters[0].Value = userID;

RunProcedure( "sp_Accounts_DeleteUser", parameters, out rowsAffected );
return (rowsAffected == 1);
}

public int ValidateLogin(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};

parameters[0].Value = userID;
parameters[1].Value = encPassword;

return RunProcedure("sp_Accounts_ValidateLogin", parameters, out rowsAffected);
}

public int TestPassword(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};

parameters[0].Value = userID;
parameters[1].Value = encPassword;

return RunProcedure("sp_Accounts_TestPassword", parameters, out rowsAffected);
}

public ArrayList GetUserRoles( string userID )
{
ArrayList roles = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;

SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetUserRoles", parameters);
while (tmpReader.Read())
{
roles.Add( tmpReader.GetString(1) );
}
Connection.Close();
return roles;
}

public ArrayList GetEffectivePermissionList( string userID )
{
ArrayList permissions = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;

SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetEffectivePermissionList", parameters);
while (tmpReader.Read())
{
permissions.Add( tmpReader.GetInt32(0) );
}
Connection.Close();
return permissions;
}

public DataSet GetUserList()
{
return RunProcedure("sp_Accounts_GetUsers", new IDataParameter[]{}, "Users");
}

public bool AddRole(string userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@RoleID", SqlDbType.Int, 4)
};
parameters[0].Value = userID;
parameters[1].Value = roleID;

RunProcedure("sp_Accounts_AddUserToRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}

public bool RemoveRole(String userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@RoleID", SqlDbType.Int,4 )
};
parameters[0].Value = userID;
parameters[1].Value = roleID;

RunProcedure("sp_Accounts_RemoveUserFromRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}

}
}


欢迎光临我的博客: http://smallfools.blog./default.html
2006-05-11 09:40
小笨笨
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:1169
专家分:0
注 册:2006-4-17
收藏
得分:0 

商务层:
using System;
using System.Data;

namespace YiPu.WebModules.Accounts.Business
{
/// <summary>
/// Users 的摘要说明。
/// </summary>
public class User : YiPu.WebModules.Business.BizObject
{
private Configuration.ModuleSettings moduleSettings;
private string userID;
private byte[] password;
private string userName;
private string company;
private string department;
private string office;
private string duties;
private bool sex;
private DateTime birth;
private string emailAddress;
private string mobile;
private DateTime registerTime;
private DateTime invalidTime;
private string question;
private string answer;
private string country;
private string province;
private string city;
private string code;
private string address;
private bool isRejected;

public User()
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
}
private void LoadFromID()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
DataRow userRow = dataUser.Retrieve( userID );
password=(byte[])userRow["Password"];
userName=userRow["UserName"].ToString();
company=userRow["Compandy"].ToString();
department=userRow["Department"].ToString();
office=userRow["Office"].ToString();
duties=userRow["Duties"].ToString();
sex=bool.Parse(userRow["Sex"].ToString());
birth=DateTime.Parse(userRow["Birth"].ToString());
emailAddress=userRow["emailAddress"].ToString();
mobile=userRow["Mobile"].ToString();
registerTime=DateTime.Parse(userRow["RegisterTime"].ToString());
invalidTime=DateTime.Parse(userRow["InvalidTime"].ToString());
question=userRow["Question"].ToString();
answer=userRow["Answer"].ToString();
country=userRow["Country"].ToString();
province=userRow["Province"].ToString();
city=userRow["City"].ToString();
code=userRow["Code"].ToString();
address=userRow["Address"].ToString();
isRejected=bool.Parse(userRow["IsRejected"].ToString());
}
public User( string existingUserID )
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
userID = existingUserID;
LoadFromID();
}

public User( IESPrincipal existingPrincipal )
{
moduleSettings = Configuration.ModuleConfig.GetSettings();
userID = ((IESIdentity)existingPrincipal.Identity).UserID;
}

public bool Create()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Create(userID,password,userName,company,department,
office,duties,sex,birth,emailAddress,mobile,registerTime,invalidTime,
question,answer,country,province,city,code,address,isRejected);
}

public bool Update()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Update(userID,userName,password,company,department,
office,duties,sex,birth,emailAddress,mobile,registerTime,invalidTime,
question,answer,country,province,city,code,address,isRejected);
}

public bool Delete()
{
Data.User dataUser=new Data.User(moduleSettings.ConnectionString);
return dataUser.Delete(userID);
}

public bool AddToRole(int RoleID)
{
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );
return dataUser.AddRole(UserID, RoleID);
}

public bool RemoveRole(int RoleID)
{
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );
return dataUser.RemoveRole( UserID, RoleID );
}

public string UserID
{
get
{
return userID;
}
set
{
userID=value;
}
}

public byte[] Password
{
get
{
return password;
}
set
{
password=value;
}
}

public string UserName
{
get
{
return userName;
}
set
{
userName=value;
}
}
public string Company
{
get
{
return company;
}
set
{
company=value;
}
}
public string Department
{
get
{
return department;
}
set
{
department=value;
}
}
public string Office
{
get
{
return office;
}
set
{
office=value;
}
}
public string Duties
{
get
{
return duties;
}
set
{
duties=value;
}
}
public bool Sex
{
get
{
return sex;
}
set
{
sex=value;
}
}
public DateTime Birth
{
get
{
return birth;
}
set
{
birth=value;
}
}
public string EmailAddress
{
get
{
return emailAddress;
}
set
{
emailAddress=value;
}
}
public string Mobile
{
get
{
return mobile;
}
set
{
mobile=value;
}
}
public DateTime RegisterTime
{
get
{
return registerTime;
}
set
{
registerTime=value;
}
}
public DateTime InvalidTime
{
get
{
return invalidTime;
}
set
{
invalidTime=value;
}
}
public string Question
{
get
{
return question;
}
set
{
question=value;
}
}
public string Answer
{
get
{
return answer;
}
set
{
answer=value;
}
}
public string Country
{
get
{
return country;
}
set
{
country=value;
}
}
public string Province
{
get
{
return province;
}
set
{
province=value;
}
}
public string City
{
get
{
return city;
}
set
{
city=value;
}
}
public string Code
{
get
{
return code;
}
set
{
code=value;
}
}
public string Address
{
get
{
return address;
}
set
{
address=value;
}
}
public bool IsRejected
{
get
{
return isRejected;
}
set
{
isRejected=value;
}
}
}
}


欢迎光临我的博客: http://smallfools.blog./default.html
2006-05-11 09:40
小笨笨
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:1169
专家分:0
注 册:2006-4-17
收藏
得分:0 

表示层
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using YiPu.WebModules.Accounts.Business;

namespace YiPu.Modules.Users
{
/// <summary>
/// Users 的摘要说明。
/// </summary>
public class Users : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button DeleteButton;
protected System.Web.UI.WebControls.DataGrid UsersGrid;

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
BindUsersGrid();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click);
this.UsersGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.UsersGrid_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void BindUsersGrid()
{
DataView dv=AccountsTool.GetUserList().Tables["Users"].DefaultView;
UsersGrid.DataSource=dv;
UsersGrid.DataBind();
}

private void UsersGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
UsersGrid.CurrentPageIndex=e.NewPageIndex;
BindUsersGrid();
}

private void DeleteButton_Click(object sender, System.EventArgs e)
{
for(int i=0;i<UsersGrid.Items.Count;i++)
{
HtmlInputCheckBox checkBox=(HtmlInputCheckBox)UsersGrid.Items[i].FindControl("CheckBox");
if(checkBox.Checked==true)
{
YiPu.WebModules.Accounts.Business.User deleteUser=new YiPu.WebModules.Accounts.Business.User();
deleteUser.UserID=checkBox.Value;
deleteUser.Delete();
}
}
BindUsersGrid();
}
}
}


欢迎光临我的博客: http://smallfools.blog./default.html
2006-05-11 09:40
guobinxian
Rank: 1
等 级:新手上路
威 望:2
帖 子:195
专家分:0
注 册:2006-3-28
收藏
得分:0 
简单的来说就是:
数据访问层,商业规则层或是实体层,表示层

?蔾錵輐落溶溶钥,柳絮筂鎕淡淡風?。
2006-05-11 14:07
jhy
Rank: 1
等 级:新手上路
帖 子:185
专家分:0
注 册:2006-4-25
收藏
得分:0 
钱途无量啊

Your future depends on your dreams !
2006-05-11 14:54
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
不错,给予支持!
2006-05-12 17:50
caiyakang
Rank: 2
等 级:新手上路
威 望:5
帖 子:2111
专家分:0
注 册:2005-3-24
收藏
得分:0 
我觉得他数据层,数据重复太多

更新,删除,插入我们完全可以用一个来实现的,就是无返回值操作.


以下是我自已通用的:
#region 无返回操作,执行如删除,更新,插入
public bool ExecuteNonQuery(bool IsPro,string CmdText)
{
try
{
con=new SqlConnection(GetConnStr());
con.Open();
cmd.Connection=con;
if(IsPro)
{
cmd.CommandType=CommandType.StoredProcedure;
}
else
{
cmd.CommandType=CommandType.Text;
}
cmd.CommandText=CmdText;
cmd.ExecuteNonQuery();
con.Close();
return true;
}
catch
{
return false;
}
}
#endregion
可为存储过程,也可是是SQL语句

中国人的财富网:http://www..cn/
2006-05-25 09:59
recycled
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-5-17
收藏
得分:0 
      
2006-06-03 15:36
下山的和尚
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-30
收藏
得分:0 
谢谢版主

2006-07-01 09:40
快速回复:求ASP.NET三层结构
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029436 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved