一、登录数据库,在数据库中创建表User00,并且插入数据。
表的字段分别为:
Id(编号)、Name(姓名)、Grid(级别)、Score(积分)、Password(密码)、Age(年龄)、Code(邀请码)。(其中编号是自动编号)
部分命令如下:
select * from User00; /*查询User00*/insert into User00 values('one','优',10000,'123',24); /*插入一行数据*/update User00 set Grid='优' where Id=001; /*更新已存在数据*/delete from User00; /*删除表里所有数据*/alter table User00 rename Code to Code; /*更改字段名*/update User00 set Code =null; /*删除某一列所有数据*/alter table User00 add Age number; /* user00中插入一列*/alter table User00 modify Age varchar2(4); /*更改某字段类型*/delete from User00 where Score is null; /*删除密码为空的所有行*/
二、新建mvc项目kaohe00,添加一个控制器Home。
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Model;using log4net;using System.Reflection;//using Bll;namespace kaohe00.Models{public class HomeController : Controller{////数据库 数据库的private static Bll.Test00 test00 = new Bll.Test00("Data Source=YWW;User Id=Test00;Password=Test00;"); //连接数据库// GET: /Home/public ActionResult Index() //显示主页的动作方法{return View();}public JsonResult ShowInfo() //把数据库里的表的数据发送到前台的方法{var list = test00.GetList(); //return Json(new { Rows = list, Total = list.Count }, JsonRequestBehavior.AllowGet);}public ActionResult Register() //注册的动作方法{return View();}}}
三、为Home的Index添加一个视图,显示主页的信息,将数据库的表User00的数据放到主页视图的表格中。
1、主页视图代码:
@{ViewBag.Title = "Index";}<script src="~/Content/jquery/jquery-1.9.0.min.js"></script><script src="~/Content/script/common.js"></script><script src="~/Content/ligerui/ligerui.all.js"></script><link href="~/Content/ligerui/skins/Aqua/css/ligerui-all.css" rel="stylesheet" /><head><title>我的主页</title></head><div id="maingrid"></div><script type="text/javascript">$(function () {$("#maingrid").ligerGrid({columns: [{ display: '编号', name: 'Id',heigth:100,width:250 },{ display: '姓名', name: 'Name', heigth: 100, width: 250 },{ display: '积分', name: 'Score', heigth: 100, width: 250 },{ display: '密码', name: 'Password', heigth: 100, width: 250 },{ display: '级别', name: 'Grid', heigth: 100, width: 250 },{ display: '邀请码', name: 'Code', heigth: 100, width: 250 }],url: "/Home/ShowInfo", //调用显示自己信息的动作方法});});</script>
2、主页视图界面:
四、实现登录功能
1、添加一个Login控制器。
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace kaohe00.Controllers{public class LoginController : Controller{//// GET: /Login///数据库private static Bll.Test00 test00 = new Bll.Test00("Data Source=YWW;User Id=Test00;Password=Test00;"); //连接数据库public ActionResult Index(){return View();}public JsonResult LoginTest(string Id ,string Password) //登录验证动作方法{var succ = test00.LoginTest(Id, Password);return Json(new { Succ = succ });}}}
2.1、为Login的Index添加一个视图
视图代码:
@{ViewBag.Title = "Index";}<script src="~/Content/jquery/jquery-1.9.0.min.js"></script><script src="~/Content/script/common.js"></script><script src="~/Content/ligerui/ligerui.all.js"></script><link href="~/Content/ligerui/skins/Aqua/css/ligerui-all.css" rel="stylesheet" /><head><title>登录</title></head><div id="login"><div id="Lform"></div></div><script type="text/javascript">$(function () {$("#Lform").ligerForm({fields: [{ display: "编号", name: "Id", newline: false, type: "text", },{ display: "密码", name: "Password", newline: true, type: "password", }],});$.ligerDialog.open({target: $("#login"),title: "登录",allowClose: false,buttons: [{text: '登录', onclick: function (item, dialog) {var form = liger.get("Lform");var data = form.getData();if(data.Id==""||data.Password==""){alert("用户名或密码不能为空");return false;}$.post("/Login/LoginTest", data, function (result) {//alert(result.Succ);if(result.Succ == true) {window.document.location.href = "/Home/Index";}else {alert("登录失败");return false;}});}},{text: '注册', onclick: function (item, dialog) {window.document.location.href = "/Register/Index";}},]});});</script>
2.2、登录视图的界面:
五、实现注册功能
1、添加一个注册控制器Register
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Model;using log4net;using System.Reflection;namespace kaohe00.Controllers{public class RegisterController : Controller{//数据库private static Bll.Test00 test00 = new Bll.Test00("Data Source=YWW;User Id=Test00;Password=Test00;");//// GET: /Register/public ActionResult Index(){return View();}public JsonResult Register(User00 user00){ var succ=test00.AddNew(user00)>0?1:0;return Json(new { Succ = succ }, JsonRequestBehavior.AllowGet);}}}
2.1、为注册控制器Register的index添加一个视图
@{ViewBag.Title = "Index";}<script src="~/Content/jquery/jquery-1.9.0.min.js"></script><script src="~/Content/script/common.js"></script><script src="~/Content/ligerui/ligerui.all.js"></script><link href="~/Content/ligerui/skins/Aqua/css/ligerui-all.css" rel="stylesheet" /><script src="scripts/jquery.validate.js" type="text/javascript"></script><head><title>注册页面</title></head><div id="reform"></div><div id="rebutton"><input style="margin-left:100px" type="button" value="注册" onclick="register()"></div><script type="text/javascript">function register() {// alert("test");var form = liger.get("reform");// alert(form.name.getData);var data = form.getData();if (data.Name == "" || data.Password == ""||data.Grid == ""){alert("请完整填写必填信息");return false;}//alert("test");$.post("/Register/Register", data,function (data) {alert("注册成功");window.document.location.href = "/Home/Index";});}$(function () {$("#reform").ligerForm({inputWidth: 170, labelWidth: 90, space: 40,fields: [{ display: "姓名 ", name: "Name", newline: true, type: "text",validate:{required:true}},{ display: "密码", name: "Password", newline: true, type: "password", type: "text", validate: { required: true } },{ display: "年龄", name: "Age", newline: true, type: "text" },{ display: "会员级别", name: "Grid", newline: true, type: "text", type: "text", validate: { required: true } },{ display: "邀请码", name: "Code", newline: true, type: "text" }],});});</script>
2.2注册视图的界面
六、为数据库的表建立Model模型实体类,建立一个类文件命名为User00.
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Model{/// <summary>/// </summary>public class User00{public int Id { get; set; }public string Name { get; set; }public string Grid { get; set; }public int Score { get; set; }public string Password { get; set; }public int Age { get; set; }public int Code { get; set; }}}
七、前文出现的Bll命名空间和类Test00等一些代码是引用了另外的库。
1、目录
2、其中文件Test00的代码:
using Blocks.Data;using Blocks.Data.CustomType;using Blocks.Data.DbProviders.Oracle;using kaohe00.Mappings;using Model;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Bll{public class Test00{/// <summary>/// 数据库/// </summary>private Database oracle = null;public Test00(string connectionString) {this.oracle = new Database(new OracleDbProvider(connectionString));this.oracle.Mappings(typeof(User00Mapping).Assembly);}public List<User00> GetList() //定义GetList函数,其功能:获得一个类型是User00类的列表相当于数组{var list = this.oracle.Select<User00>().ToList();return list;}public int AddNew(User00 user00){return this.oracle.Insert(user00);}public bool LoginTest(string Id,string Password) //函数功能:判断前台穿的值是否在数据库中的{// var search = this.oracle.Select<User00>();// var list = search.Where(t => t.Id == int.Parse(Id)) && t.Password == Password; var search = this.oracle.Select<User00>().Where(t => t.Id == int.Parse(Id) && t.Password == Password);var list = search.ToList(); //list相当于数组if (list.Count > 0) //??!!{//var user = list.First();return true;}else{return false;}}}}
3、其中的kaohe00.Mappings文件里的User00Mapping.cs的文件的代码:
using Blocks.Data.Mapping;using Model;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace kaohe00.Mappings{public class User00Mapping : ClassMap<User00>{public User00Mapping() {Map(t => t.Id).AutoNumber();Map(t => t.Name);}}}
八、设置路径: defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },使其先执行Login。
九、查看效果:
1、点击登录后密码错误的情况:
或者
2、输入正确的编号密码,进入主页视图界面
3、点击注册后进入注册视图界面
4、在注册界面输入内容,注册失败和成功的情况:
或者
注册成功后点击确定,进入主页视图界面
可以看到主页视图界面新添加的信息
好了,关于mvc C# JavaScript LigerUI oracle实现用户的注册、登陆验证、登陆 的内容就给大家介绍到这里,希望对大家有所帮助!
新闻热点
疑难解答