使用EF调用存储过程实现登录功能

视图代码

<h2>学生登录界面</h2>
@using (Html.BeginForm())
{
<div id="dlu">

	<div class=" row">
		<div class="col-lg-6">
			<input type="text" placeholder="请输入账号" name="StuLoginName" class="form-control" id="StuLoginName" />
		</div>
		<div class="col-lg-6">
			<input type="password" placeholder="请输入密码" name="StuLoginPwd" class="form-control" id="StuLoginPwd" />
		</div>
	</div>
	<div class=" row">
		<div class="col-lg-offset-2 col-lg-8">
			<img src="~/img/26a0uw36.bmp" style="display:none" id="ajaxLoading" />
			<input type="submit" value="登录" class="form-control btn btn-primary" />
		</div>
	</div>
</div>
}

存储过程

ALTER proc [dbo].[proc_IsLogin]
@sName nvarchar(20),
@sPwd nvarchar(20),
@result int output
as
if exists(select * from Student where StuLoginName=@sName and StuLoginPwd=@sPwd)
set @result=1   --表示登录成功
else
set @result=0   --表示登录失败

DAL层

/// <summary>
		/// 通过用户名和账号查询学生
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static int select(string name,string pwd)
		{
			using (ExamDBEntities1 db = new ExamDBEntities1())
			{
				//定义一个输出参数
				ObjectParameter paral = new ObjectParameter("result", typeof(int));
				//调用一下存储过程
				var result = db.proc_IsLogin(name, pwd, paral);
				//返回结果
				return (int)paral.Value;

			}
		}

BLL层

public static bool CheckLogin(string name, string pwd)
		{
			var tea = DAL.StudentService.select(name,pwd);
			if (tea == 0)
			{
				return false;
			}
			return true;
		}

控制器

  /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Student student)
        {
            if (ModelState.IsValidField("StuLoginName") && ModelState.IsValidField("StuLoginPwd"))
            {
                if (StuMAc.CheckLogin1(student.StuLoginName, student.StuLoginPwd))
                {
                    Session["loginName"] = student.StuLoginName;
                    return RedirectToAction("Index");
                }
                else { return Content("<script>alert('登录失败');location.href='/Student/Login'</script>"); }
            }
            else return View();
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值