一、登录数据库,在数据库中创建表user00,并且插入数据。
表的字段分别为:
id(编号)、name(姓名)、grid(级别)、score(积分)、password(密码)、age(年龄)、code(邀请码)。(其中编号是自动编号)
部分命令如下:
1
2
3
4
5
6
7
8
9
|
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。
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
|
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、主页视图代码:
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
|
@{ 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控制器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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添加一个视图
视图代码:
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
|
@{ 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
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
|
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添加一个视图
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
|
@{ 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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的代码:
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
|
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的文件的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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实现用户的注册、登陆验证、登陆 的内容就给大家介绍到这里,希望对大家有所帮助!