三层架构用户登录代码c语言,网站用户登录、注册和修改密码常用代码,采用三层架构...

创建用户表的Sql语句,数据库为SQL Server2000:

create table "User" (

UserID int not null,

UserName varchar(50) null,

UserPwd varchar(50) null,

UserCode varchar(50) null,

UserRole varchar(50) null,

CreatTime datetime null default getdate(),

UpdateTime datetime null default getdate(),

Define1 varchar(50) null,

Define2 varchar(50) null,

constraint PK_USER primary key nonclustered (UserID)

)

go

1.用户登录功能的实现:

已经使用LiTianPing的动软代码生成器生成了三层架构,以下是添加的代码。

(1)DAL层的User.cs中添加两个方法:

///

/// 用户登录

///

///

///

///

public HJMS.Model.User CheckUser(string userName,string password)

{

HJMS.Model.User userInfo=new Model.User();

int num = 0;string str = "";

StringBuilder strSql = new StringBuilder();

strSql.Append(" select top 1 UserID,UserName,UserPwd from User");

strSql.Append(" where UserName=@UserName");

SqlParameter[] pt = {new SqlParameter("@UserName", SqlDbType.VarChar),};

pt[0].Value = userName.ToLower();

using(SqlDataReader reader=DbHelperSQL.ExecuteReader(strSql.ToString(),pt))

{

if(reader.Read())

{

num = reader.GetInt32(0);

str = reader[2].ToString();

}

}

if(num!=0)

{

if(str==password)

{

userInfo.UserPwd = password;

StringBuilder strSql2 = new StringBuilder();

strSql.Append(" select top 1 UserID,UserName,UserPwd,UserCode,UserRole,CreatTime,UpdateTime,Define1,Define2 from User");

strSql.Append(" where UserID=@UserID");

SqlParameter[] parameters2 = {new SqlParameter("@PerID", SqlDbType.Int,4)};

parameters2[0].Value = num;

using (SqlDataReader reader2 = DbHelperSQL.ExecuteReader(strSql2.ToString(), parameters2))

{

if (reader2.Read())

{

userInfo.UserID = reader2[0] == null ? 0 : reader2.GetInt32(0);

userInfo.UserName = reader2[1].ToString();

userInfo.UserCode = reader2[3].ToString();

userInfo.UserRole = reader2[4].ToString();

userInfo.CreatTime = reader2.GetDateTime(5);

userInfo.UpdateTime = reader2.GetDateTime(6);

userInfo.Define1 = reader2[7].ToString();

userInfo.Define2 = reader2[8].ToString();

}

}

}

}

return userInfo;

}

///

/// 根据用户名获得用户实体

///

///

///

public HJMS.Model.User GetModelByUserName(string userName)

{

StringBuilder strSql = new StringBuilder();

strSql.Append(" select top 1 UserID,UserName,UserPwd,UserCode,UserRole,CreatTime,UpdateTime,Define1,Define2 from User");

strSql.Append(" where UserName=@UserName");

SqlParameter[] pt = {new SqlParameter("@UserName", SqlDbType.VarChar)};

pt[0].Value = userName;

HJMS.Model.User user = new Model.User();

DataTable dtUser = DbHelperSQL.Query(strSql.ToString(), pt).Tables[0];

if (dtUser.Rows.Count > 0)

{

if (dtUser.Rows[0]["UserID"].ToString() != "")

{

user.UserID = int.Parse(dtUser.Rows[0]["UserID"].ToString());

}

user.UserName = dtUser.Rows[0]["UserName"].ToString();

user.UserPwd = dtUser.Rows[0]["UserPwd"].ToString();

user.UserCode = dtUser.Rows[0]["UserCode"].ToString();

user.UserRole = dtUser.Rows[0]["UserRole"].ToString();

user.CreatTime = Convert.ToDateTime(dtUser.Rows[0]["CreateTime"]);

user.UpdateTime = Convert.ToDateTime(dtUser.Rows[0]["UpdateTime"]);

user.Define1 = dtUser.Rows[0]["Define1"].ToString();

user.Define2 = dtUser.Rows[0]["Define2"].ToString();

return user;

}

else

{

return null;

}

}

(2) BLL层的User.cs中添加两个方法:

///

/// 用户登录

///

///

///

///

public HJMS.Model.User CheckUser(string userName, string password)

{

return dal.CheckUser(userName, password);

}

///

/// 根据用户名获得用户实体

///

///

///

public HJMS.Model.User GetModelByUserName(string userName)

{

return dal.GetModelByUserName(userName);

}

(3)UI层就是用户界面,假设Login.aspx如下的话:

系统登录

用户名:

EnableTheming="false">

密 码:

class="password" EnableTheming="false">

IE ()

那么Login.aspx.cs页面就可以这样:

public partial class Login : System.Web.UI.Page

{

private HJMS.BLL.User userBll = new HJMS.BLL.User();

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnLogin_Click(object sender, EventArgs e)

{

HJMS.Model.User UserInfo = userBll.CheckUser(txtUserID.Text.Trim(), txtPwd.Text.Trim());

if(UserInfo!=null&&UserInfo.UserID!=0)

{

HttpCookie cookie = FormsAuthentication.GetAuthCookie(UserInfo.UserName, true);

cookie.Expires = DateTime.Now.AddDays(7);

Response.Cookies.Add(cookie);

if(Request.QueryString["ReturnUrl"]!=null)

{

Context.Response.Redirect(Request.QueryString["ReturnUrl"]);// 重定向到用户申请的初始页面

}

else

{

Response.Redirect("Admin/Navigator.aspx");

}

}

else

{

Page.ClientScript.RegisterStartupScript(Page.GetType(),"Fails","alert('用户名或密码错误!');",true);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值