9个回答
展开全部
模型层
private int userId;
public int UserId
{
get { return userId; }
set { userId = value; }
}
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value; }
}
数据层
public class UserService
{
public List<User> GetAllUserDynamic(string whereCondition)
{
List<User> users=new List<User>();
string strSQL = "spSelect";
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@WhereCondition",whereCondition),
new SqlParameter("@OrderCondition",null)
};
using (SqlDataReader dataReader=SqlHelper.GetDataReader(strSQL,parameter))
{
while (dataReader.Read())
{
User user = new User();
user.UserId = (int)dataReader["UserId"];
user.UserName = dataReader["UserName"].ToString();
user.PassWord = dataReader["PassWord"].ToString();
// user.TypeId = (int)dataReader["TypeId"];
users.Add(user);
}
}
return users;
}
public object AddUser(User user)
{
string strSQL = "spInsert";
string valueCondition = string.Format("'{0}','{1}',{2}",user.UserName,user.PassWord,user.TypeId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@ValueCondition",valueCondition)
};
return SqlHelper.ExecuteSalar(strSQL, parameter);
}
public int UpdateUser(User user)
{
string strSQL = "spUpdate";
string setCondition = string.Format("[UserName]='{0}',[PassWord]='{1}',[TypeId]={2}", user.UserName, user.PassWord, user.TypeId);
string whereCondition = string.Format("[UserId]={0}",user.UserId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@SetCondition",setCondition),
new SqlParameter("@WhereCondition",whereCondition)
};
return SqlHelper.ExecuteCommand(strSQL, parameter);
}
public int DeleteUser(User user)
{
string strSQL = "spDelete";
string whereCondition = string.Format("[UserId]={0}", user.UserId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@WhereCondition",whereCondition)
};
return SqlHelper.ExecuteCommand(strSQL, parameter);
}
}
业务层
public class UserManager
{
UserService userService=new UserService();
[DataObjectMethod(DataObjectMethodType.Select)]
public List<User> GetAllUser()
{
return userService.GetAllUserDynamic(null);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public User GetAllUserByUserId(int userId)
{
string whereCondition = string.Format("[UserId]={0}",userId);
List<User> users = userService.GetAllUserDynamic(whereCondition);
return users.Count > 0 ? users[0] : null;
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public object AddUser(User user)
{
return userService.AddUser(user);
}
[DataObjectMethod(DataObjectMethodType.Update)]
public int UpdateUser(User user)
{
return userService.UpdateUser(user);
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public int DeleteUser(User user)
{
return userService.DeleteUser(user);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public User LoginUser(object userName,object passWord)
{
string whereCondition = string.Format("[UserName]='{0}'AND [PassWord]='{1}'",userName,passWord);
List<User> users = userService.GetAllUserDynamic(whereCondition);
return users.Count > 0 ? users[0] : null;
}
}
表示层 随便拖个拖个gridview 绑定下数据源
我用的通用存储过程
CREATE PROCEDURE dbo.spSelect
(
@TableCondition nvarchar(255),
@WhereCondition nvarchar(255)=null,
@OrderCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='SELECT * FROM['+@TableCondition+']'
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
IF @OrderCondition IS NOT NULL AND LEN (@OrderCondition)>0
BEGIN
SET @SQL=@SQL+'ORDER BY'+@OrderCondition
END
EXEC sp_executesql @SQL
RETURN
GO
CREATE PROCEDURE dbo.spInsert
(
@TableCondition nvarchar(255),
@ValueCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='INSERT INTO['+@TableCondition+']'
IF @ValueCondition IS NOT NULL AND LEN (@ValueCondition)>0
BEGIN
SET @SQL=@SQL+'VALUES('+@ValueCondition+')'
END
SET @SQL=@SQL+'SELECT SCOPE_IDENTITY()'
EXEC sp_executesql @sql
RETURN
GO
CREATE PROCEDURE dbo.spUpdate
(
@TableCondition nvarchar(255),
@SetCondition nvarchar(255)=null,
@WhereCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='UPDATE ['+@TableCondition+']'
IF @SetCondition IS NOT NULL AND LEN (@SetCondition)>0
BEGIN
SET @SQL=@SQL+'SET'+@SetCondition
END
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
EXEC sp_executesql @sql
RETURN
GO
CREATE PROCEDURE dbo.spDelete
(
@TableCondition nvarchar(255),
@WhereCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='DELETE FROM ['+@TableCondition+']'
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
EXEC sp_executesql @sql
RETURN
private int userId;
public int UserId
{
get { return userId; }
set { userId = value; }
}
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value; }
}
数据层
public class UserService
{
public List<User> GetAllUserDynamic(string whereCondition)
{
List<User> users=new List<User>();
string strSQL = "spSelect";
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@WhereCondition",whereCondition),
new SqlParameter("@OrderCondition",null)
};
using (SqlDataReader dataReader=SqlHelper.GetDataReader(strSQL,parameter))
{
while (dataReader.Read())
{
User user = new User();
user.UserId = (int)dataReader["UserId"];
user.UserName = dataReader["UserName"].ToString();
user.PassWord = dataReader["PassWord"].ToString();
// user.TypeId = (int)dataReader["TypeId"];
users.Add(user);
}
}
return users;
}
public object AddUser(User user)
{
string strSQL = "spInsert";
string valueCondition = string.Format("'{0}','{1}',{2}",user.UserName,user.PassWord,user.TypeId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@ValueCondition",valueCondition)
};
return SqlHelper.ExecuteSalar(strSQL, parameter);
}
public int UpdateUser(User user)
{
string strSQL = "spUpdate";
string setCondition = string.Format("[UserName]='{0}',[PassWord]='{1}',[TypeId]={2}", user.UserName, user.PassWord, user.TypeId);
string whereCondition = string.Format("[UserId]={0}",user.UserId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@SetCondition",setCondition),
new SqlParameter("@WhereCondition",whereCondition)
};
return SqlHelper.ExecuteCommand(strSQL, parameter);
}
public int DeleteUser(User user)
{
string strSQL = "spDelete";
string whereCondition = string.Format("[UserId]={0}", user.UserId);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@TableCondition","User"),
new SqlParameter("@WhereCondition",whereCondition)
};
return SqlHelper.ExecuteCommand(strSQL, parameter);
}
}
业务层
public class UserManager
{
UserService userService=new UserService();
[DataObjectMethod(DataObjectMethodType.Select)]
public List<User> GetAllUser()
{
return userService.GetAllUserDynamic(null);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public User GetAllUserByUserId(int userId)
{
string whereCondition = string.Format("[UserId]={0}",userId);
List<User> users = userService.GetAllUserDynamic(whereCondition);
return users.Count > 0 ? users[0] : null;
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public object AddUser(User user)
{
return userService.AddUser(user);
}
[DataObjectMethod(DataObjectMethodType.Update)]
public int UpdateUser(User user)
{
return userService.UpdateUser(user);
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public int DeleteUser(User user)
{
return userService.DeleteUser(user);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public User LoginUser(object userName,object passWord)
{
string whereCondition = string.Format("[UserName]='{0}'AND [PassWord]='{1}'",userName,passWord);
List<User> users = userService.GetAllUserDynamic(whereCondition);
return users.Count > 0 ? users[0] : null;
}
}
表示层 随便拖个拖个gridview 绑定下数据源
我用的通用存储过程
CREATE PROCEDURE dbo.spSelect
(
@TableCondition nvarchar(255),
@WhereCondition nvarchar(255)=null,
@OrderCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='SELECT * FROM['+@TableCondition+']'
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
IF @OrderCondition IS NOT NULL AND LEN (@OrderCondition)>0
BEGIN
SET @SQL=@SQL+'ORDER BY'+@OrderCondition
END
EXEC sp_executesql @SQL
RETURN
GO
CREATE PROCEDURE dbo.spInsert
(
@TableCondition nvarchar(255),
@ValueCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='INSERT INTO['+@TableCondition+']'
IF @ValueCondition IS NOT NULL AND LEN (@ValueCondition)>0
BEGIN
SET @SQL=@SQL+'VALUES('+@ValueCondition+')'
END
SET @SQL=@SQL+'SELECT SCOPE_IDENTITY()'
EXEC sp_executesql @sql
RETURN
GO
CREATE PROCEDURE dbo.spUpdate
(
@TableCondition nvarchar(255),
@SetCondition nvarchar(255)=null,
@WhereCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='UPDATE ['+@TableCondition+']'
IF @SetCondition IS NOT NULL AND LEN (@SetCondition)>0
BEGIN
SET @SQL=@SQL+'SET'+@SetCondition
END
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
EXEC sp_executesql @sql
RETURN
GO
CREATE PROCEDURE dbo.spDelete
(
@TableCondition nvarchar(255),
@WhereCondition nvarchar(255)=null
)
AS
DECLARE @SQL NVARCHAR(255)
SET @SQL='DELETE FROM ['+@TableCondition+']'
IF @WhereCondition IS NOT NULL AND LEN (@WhereCondition)>0
BEGIN
SET @SQL=@SQL+'WHERE'+@WhereCondition
END
EXEC sp_executesql @sql
RETURN
展开全部
去我的百度空间看看,我转了两篇关于asp.net三层架构的简单实例!!!
我一开始也是看书,看文字搞得好乱,最后看了这两个实例才基本搞明白。
http://hi.baidu.com/syranmo/blog/item/5e11510bbe1bb035b0351d04.html
还有
http://hi.baidu.com/syranmo/blog/item/fb7ac4cf6210d932b700c804.html
希望对你有帮助!
我一开始也是看书,看文字搞得好乱,最后看了这两个实例才基本搞明白。
http://hi.baidu.com/syranmo/blog/item/5e11510bbe1bb035b0351d04.html
还有
http://hi.baidu.com/syranmo/blog/item/fb7ac4cf6210d932b700c804.html
希望对你有帮助!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单的你就不会完全领会“三层架构”的内涵
下载微软的petshop看下吧
下载微软的petshop看下吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考答案:
三层架构一般指的是界面层,业务层,数据层。
界面层就是系统的操作界面,和用户直接交互的地方。
业务层又称为逻辑层,英文名称是Business Logic Layer,简称BLL,是执行业务逻辑的地方,也就是业务规则都在这一层体现。
数据层也称为数据访问层,英文名称是Database Access Layer,简称DAL,这里是直接和数据库进行交互的地方,也是整个系统里唯一允许访问数据库的地方。
除此之外,还有一个业务实体层,这个层比较特殊,就是定义实体类的地方,有些人把这个层归属业务层,有些人把这个独立出来,变成一个公共层。
各个层次之间的访问关系:
三层架构一般指的是界面层,业务层,数据层。
界面层就是系统的操作界面,和用户直接交互的地方。
业务层又称为逻辑层,英文名称是Business Logic Layer,简称BLL,是执行业务逻辑的地方,也就是业务规则都在这一层体现。
数据层也称为数据访问层,英文名称是Database Access Layer,简称DAL,这里是直接和数据库进行交互的地方,也是整个系统里唯一允许访问数据库的地方。
除此之外,还有一个业务实体层,这个层比较特殊,就是定义实体类的地方,有些人把这个层归属业务层,有些人把这个独立出来,变成一个公共层。
各个层次之间的访问关系:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,我是张波老师,网上有我发的一套.NET的三层(N层)架构的OA项目视频,60小时.
里有面有详细的搭建过程,也许对你有帮助.
里有面有详细的搭建过程,也许对你有帮助.
参考资料: http://www.beingnet.com:8080/showtopic-23.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询