使用MVC实现登录功能
首先,从底层开始即Models:
(1)通用数据访问类(封装数据访问类方法):SqlHelper类
使用命名空间:using System.Data;
using System.Data.SqlClient;
using System.Configuration;//使用配置文件Web.config所需的命名空间
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace mvcDemo.Models { /// <summary> /// (1)通用访问类(封装数据访问类方法) /// </summary> public class SqlHelper { //定义链接字符串 private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();//使用配置文件 public static SqlDataReader GetReader(string sql) { SqlConnection con = new SqlConnection(connString); SqlCommand com = new SqlCommand(sql, con); try { con.Open(); return com.ExecuteReader(CommandBehavior.CloseConnection);//dataReader关闭后自动关闭 } catch (Exception ex) { //写出错误日志 throw ex; } } } }
web.config:配置为
<connectionStrings>
<add name="connString" connectionString="Server=.;DataBase=DBFirst;uid=sa;pwd=123456"/>//数据库连接字符串
</connectionStrings>
(2)数据访问类:实体名称+Service,需使用命名空间:
using System.Data;
using System.Data.SqlClient;
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; namespace mvcDemo.Models { /// <summary> ///(2) 数据访问类 /// </summary> public class sysAdminService {/// <summary> /// 根据账号和密码登录 /// </summary> /// <param name="objAdmin"></param> /// <returns></returns> public sysAdmin AdminLogin(sysAdmin objAdmin)//返回对象思考??及对参数为实体的思考?? { string sql = "select LoginName from First where LoginId={0}and LoginPwd='{1}'"; sql = string.Format(sql,objAdmin.LoginId,objAdmin.Loginpwd);//格式化 SqlDataReader objReader = SqlHelper.GetReader(sql); if (objReader.Read()) { objAdmin.LoginName = objReader["LoginName"].ToString(); } else { objAdmin = null; } objReader.Close(); return objAdmin; } } }
(3)实体类
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace mvcDemo.Models { /// <summary> /// 实体类 /// </summary> public class sysAdmin { public int LoginId { get; set; } public string LoginName { get; set; } public string Loginpwd { get; set; } } }
其次,Views()【视图层】使用form提交
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>登录</title> </head> <body> <form method="post" action="/sysAdmin/AdminLogin"> 用户名:<input type="text" name="LoginId" /> <br /><br /> 密码:<input type="password" name="Loginpwd"/> <br /><br /> <input type="submit" value="登录" /><br /> <span style="color:red">@ViewData["info"]</span> </form> </body> </html>
最后,Controller(控制器)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using mvcDemo.Models; namespace mvcDemo.Controllers { public class sysAdminController : Controller { public ActionResult Index()//呈现视图 { return View(); } //编写动作方法 public ActionResult AdminLogin() { //[1] 获取数据 int loginId = Convert.ToInt32(Request.Params["LoginId"]); string loginpwd = Request.Params["Loginpwd"];//接受form提交的数据 sysAdmin objAdmin = new sysAdmin()
{ LoginId = loginId, Loginpwd = loginpwd };//对象初始化器(对属性赋值) //[2] 业务处理 掉用数据访问类 使用数据访问类中的方法 objAdmin = new sysAdminService().AdminLogin(objAdmin); if (objAdmin!=null) { ViewData["info"] = "欢迎登录!"+objAdmin.LoginName; } else { ViewData["info"] = "用户名或密码错误"; } //[3] 返回数据(视图) return View("Index"); } } }
注意:数据库的创建,若没有则报错
As you wish.