Asp.net三层架构

Asp.net三层架构

Model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class L_User
{
#region 实例化用户&管理员的主要字段
public int UID { get ; set ; }
public string Author { get ; set ; }
public string UUserPwd { get ; set ; }
public string UEmail { get ; set ; }
public DateTime UCreateTime { get ; set ; }
public string UUserRole { get ; set ; }
#endregion
}
}


DAL:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
public class UserDAL
{
//添加用户
public int AddUser(L_User user)
{
try
{
int i = DBHelper.executeSql( "insert into L_User(Author,UUserPwd,UEmail,UCreateTime,UUserRole) values(@Author,@UUserPwd,@UEmail,@UCreateTime,@UUserRole)" ,
new SqlParameter( "@Author" , user.Author), new SqlParameter( "@UUserPwd" , user.UUserPwd),
new SqlParameter( "@UEmail" , user.UEmail), new SqlParameter( "@UCreateTime" , user.UCreateTime),
new SqlParameter( "@UUserRole" , user.UUserRole));
return i;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
}
//修改user
public int UpdateUser( int UID, string Author, string UUserPwd, string UEmail, DateTime UCreateTime, string UUserRole)
{
int i = DBHelper.executeSql( "update L_User set Author=@Author,UUserPwd=@UUserPwd,UEmail=@UEmail, UCreateTime=@UCreateTime,UUserRole=@UUserRole where UID=@UID" ,
new SqlParameter( "@UID" , UID),
new SqlParameter( "@Author" , Author),
new SqlParameter( "@UUserPwd" , UUserPwd),
new SqlParameter( "@UEmail" , UEmail),
new SqlParameter( "@UCreateTime" , UCreateTime),
new SqlParameter( "@UUserRole" , UUserRole));
return i;
}
//删除user
public int DeleteUser( int UID)
{
int i = DBHelper.executeSql( "delete from L_User where UID=@UID " , new SqlParameter( "@UID" , UID));
return i;
}
//单独的一条user信息
public L_User get_singleUser( int UID)
{
DataSet ds = DBHelper.dataset( "select * from L_User where UID=@UID " , new SqlParameter( "@UID" , UID));
L_User lu = new L_User();
if (ds != null )
{
lu.UID = Convert.ToInt32(ds.Tables[0].Rows[0][ "UID" ].ToString());
lu.Author = ds.Tables[0].Rows[0][ "Author" ].ToString();
lu.UUserPwd = ds.Tables[0].Rows[0][ "UUserPwd" ].ToString();
lu.UEmail = ds.Tables[0].Rows[0][ "UEmail" ].ToString();
lu.UCreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0][ "UCreateTime" ].ToString());
lu.UUserRole = ds.Tables[0].Rows[0][ "UUserRole" ].ToString();
return lu;
}
else
{
return null ;
}
}
//单独的一条user信息2
public L_User get_singleUser( string name)
{
DataSet ds = DBHelper.dataset( "select * from L_User where Author=@Author " , new SqlParameter( "@Author" , name));
L_User lu = new L_User();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
lu.UID = Convert.ToInt32(ds.Tables[0].Rows[0][ "UID" ].ToString());
lu.Author = ds.Tables[0].Rows[0][ "Author" ].ToString();
lu.UUserPwd = ds.Tables[0].Rows[0][ "UUserPwd" ].ToString();
lu.UEmail = ds.Tables[0].Rows[0][ "UEmail" ].ToString();
lu.UCreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0][ "UCreateTime" ].ToString());
lu.UUserRole = ds.Tables[0].Rows[0][ "UUserRole" ].ToString();
return lu;
}
else
{
return null ;
}
}
// 获取所有用户的列表信息
public DataSet GetUserGroupList2( string sqlstr)
{
string cmdText = "select * from L_User where 1=1 " ;
if (sqlstr != "" )
{
cmdText += sqlstr;
}
return DBHelper.getDataSet(cmdText);
}
//修改密码
public int UpdateUS( string UUserPwd)
{
int i = DBHelper.executeSql( @" update L_User set UUserPwd=@UUserPwd ;" , new SqlParameter( "@UUserPwd" , UUserPwd));
return i;
}
//检查登录(此方法有误)
public int CheckLogin( string Author, string UUserPwd)
{
try
{
int i = Convert.ToInt32(DBHelper.executeScalar( "select count(*) from L_User where Author=@Author and UUserPwd=@UUserPwd " , new SqlParameter( "@Author" , Author), new SqlParameter( "@UUserPwd" , UUserPwd)));
return i;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
}
//验证用户的角色
public L_User checkAuthor( string Author)
{
DataSet ds = DBHelper.dataset( "select * from L_User where Author=@Author " , new SqlParameter( "@Author" , Author));
if (ds != null )
{
L_User LU = new L_User();
LU.UUserRole = ds.Tables[0].Rows[0][ "UUserRole" ].ToString();
return LU;
}
else
{
return null ;
}
}
//验证用户是否相同
public int CheckUser( string Author)
{
try
{
int i = Convert.ToInt32(DBHelper.executeScalar( "select count(*) from L_User where Author=@Author" , new SqlParameter( "@Author" , Author)));
return i;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
}
//验证旧密码是否相同
public L_User Checkjiu( string Author)
{
DataSet ds = DBHelper.dataset( "select * from L_User where Author=@Author " , new SqlParameter( "@Author" , Author));
L_User LU = new L_User();
LU.UUserPwd = ds.Tables[0].Rows[0][ "UUserPwd" ].ToString();
return LU;
}
}
}


BLL:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Data;
namespace BLL
{
public class UserService
{
/// <summary>
/// 添加用户
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int AddUser(L_User user)
{
UserDAL userDal = new UserDAL();
return userDal.AddUser(user);
}
/// <summary>
/// 修改user
/// </summary>
/// <param name="UID"></param>
/// <param name="BlogTiltle"></param>
/// <param name="BlogContent"></param>
/// <param name="CreateTime"></param>
/// <param name="Recommand"></param>
/// <returns></returns>
public int UpdateUser( int UID, string Author, string UUserPwd, string UEmail, DateTime UCreateTime, string UUserRole)
{
UserDAL uDAL = new UserDAL();
return uDAL.UpdateUser(UID, Author, UUserPwd, UEmail, UCreateTime, UUserRole);
}
/// <summary>
/// 删除user
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public int DeleteUser( int UID)
{
UserDAL uDAL = new UserDAL();
return uDAL.DeleteUser(UID);
}
/// <summary>
/// 获取所有用户的信息列表
/// </summary>
/// <returns></returns>
public DataSet GetUserGroupList2( string sqlstr)
{
UserDAL ugDAL = new UserDAL();
return ugDAL.GetUserGroupList2(sqlstr);
}
/// <summary>
/// 单独的一条用户信息
/// </summary>
/// <param name="UID"></param>
/// <returns></returns>
public L_User get_singleUser( int UID)
{
UserDAL uDAL = new UserDAL();
return uDAL.get_singleUser(UID);
}
/// <summary>
/// 查找login用户名,实现登录的安全验证
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public L_User get_singleUser( string name)
{
UserDAL uDAL = new UserDAL();
return uDAL.get_singleUser(name);
}
/// <summary>
/// 修改密码
/// </summary>
/// <param name="AUserPwd"></param>
/// <returns></returns>
public int UpdateUS( string UUserPwd)
{
UserDAL uDAL = new UserDAL();
return uDAL.UpdateUS(UUserPwd);
}
/// <summary>
/// 检查登录(此方法有误)
/// </summary>
/// <param name="Author"></param>
/// <param name="UUserPwd"></param>
/// <param name="UUserRole"></param>
/// <returns></returns>
public int CheckLogin( string Author, string UUserPwd)
{
UserDAL userDal = new UserDAL();
return userDal.CheckLogin(Author, UUserPwd);
}
/// <summary>
/// 检查用户权限
/// </summary>
/// <param name="Author"></param>
/// <returns></returns>
public L_User checkAuthor( string Author)
{
UserDAL userDal = new UserDAL();
return userDal.checkAuthor(Author);
}
/// <summary>
/// 检查用户名
/// </summary>
/// <param name="LoginName"></param>
/// <returns></returns>
public int CheckUser( string Author)
{
UserDAL userDal = new UserDAL();
return userDal.CheckUser(Author);
}
/// <summary>
/// 验证旧密码是否相同
/// </summary>
/// <param name="Author"></param>
/// <returns></returns>
public L_User Checkjiu( string Author)
{
UserDAL uDAL = new UserDAL();
return uDAL.Checkjiu(Author);
}
}
}


接下来我们就可以开始在web页面cs中开始我们的功能调用了,实现编辑功能的一部分代码如下,PS:验证input的

话建议大家多写一些,JS在前台,本页面的验证,以及服务器端的验证,保证信息的安全性,养成好习惯。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
protected void ImageButton1_Click( object sender, ImageClickEventArgs e)
{
if (TextBox1.Text.Trim().ToString() == "" )
{
lblMessage.Text = "<font color=\"red\" size=\"2\">请输入用户名</font>" ;
NormalMethod.Show( this , "请完整填写用户的信息" );
return ;
}
if (pwd.Text.Trim().ToString() == "" )
{
lblMessage.Text = "<font color=\"red\" size=\"2\">请输入用户密码</font>" ;
NormalMethod.Show( this , "请完整填写用户的信息" );
return ;
}
if (TextBox3.Text.Trim().ToString() == "" )
{
lblMessage.Text = "<font color=\"red\" size=\"2\">请输入用户邮箱</font>" ;
NormalMethod.Show( this , "请完整填写用户的信息" );
return ;
}
if (ddlGroup.SelectedIndex == 0)
{
lblMessage.Text = "<font color=\"red\" size=\"2\">请输入用户组</font>" ;
NormalMethod.Show( this , "请完整填写用户的信息" );
return ;
}
if (my97.Value.ToString() == "" )
{
lblMessage.Text = "<font color=\"red\" size=\"2\">请输入正确的日期格式</font>" ;
NormalMethod.Show( this , "请完整填写用户的信息" );
return ;
}
UserService uService = new UserService();
if (editor == "edit" )
{
int i = uService.UpdateUser(Convert.ToInt32(Session[ "UID" ].ToString()), TextBox1.Text.ToString(), pwd.Text.ToString(), TextBox3.Text.ToString(), DateTime.Now, ddlGroup.Text.ToString());
if (i > 0)
{
NormalMethod.ShowAndRedirect( this , "修改成功!(*^__^*)" , "UserGuan.aspx" );
editor = null ;
}
else
{
NormalMethod.ShowAndRedirect( this , "修改失败!#(┬_┬)我的错,我改还不行吗?" , "UserGuan.aspx" );
}
}
else
{
TextBox1.ReadOnly = false ;
TextBox3.ReadOnly = false ;
my97.Visible = true ;
L_User lu = new L_User();
lu.Author = TextBox1.Text.ToString();
lu.UUserPwd = pwd.Text.ToString();
lu.UEmail = TextBox3.Text.ToString();
lu.UUserRole = ddlGroup.SelectedValue.ToString();
lu.UCreateTime =Convert.ToDateTime(my97.Value.ToString());
int j = uService.AddUser(lu);
if (j > 0)
{
NormalMethod.ShowAndRedirect( this , "添加成功!(*^__^*)" , "UserGuan.aspx" );
TextBox1.Text = "" ;
TextBox3.Text = "" ;
my97.Value = "" ;
pwd.Text = "" ;
}
else
{
NormalMethod.ShowAndRedirect( this , "添加失败!#(┬_┬)我的错,我改还不行吗?" , "UserGuan.aspx" );
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值