设为首页 加入收藏

TOP

以对象的方式来访问xml数据表(二)(四)
2019-09-03 03:27:41 】 浏览:114
Tags:对象 方式 访问 xml 数据
del里面的User类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        [Required]
        public Int32 Id { get; set; }

        [Required]
        [DisplayName("名字")]
        public String Name { get; set; }

        [Required]
        [DisplayName("用户名")]
        public String Account { get; set; }

        [Required]
        [DisplayName("密码")]
        public String Password { get; set; }

        //创建时间
        [Required]
        public DateTime CreateTime { get; set; }

        //标识是否为管理员
        [Required]
        public Boolean IsAdmin { get; set; }

    }
}
View Code

  继承了DbContext接口的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Test.Models
{
    public class WS_WebAppContext : DbContext
    {

        public virtual DbSet<User> Users { get; set; }

        public WS_WebAppContext() : base("name=WS_WebAppContext")
        {
            
        }
    }
}
View Code

  Control里面轻松访问,只是给出了登录验证部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Test.Models;

namespace Test.Controllers
{
    public class HomeController : Controller
    {
        
        WS_WebAppContext entity = new WS_WebAppContext();
        
         //登录页面
        public ActionResult Login()
        {
            return View();
        }
        
        //检查登录信息
        [HttpPost]
        public ActionResult Login(User u)
        {
            var logined = entity.Users.SingleOrDefault(m => m.Account == u.Account);
            if (!string.IsNullOrWhiteSpace(u.Password) && logined != null && logined.Password == u.Password)
            {
                String role = "User";
                if (logined.IsAdmin)
                {
                    role = "Admin";
                }
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                    1,
                    logined.Id.ToString(),
                    DateTime.Now,
                    DateTime.Now.AddMinutes(120),
                    false,
                    role
                   );
                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                Response.Cookies.Add(authCookie);
                if (logined.IsAdmin)
                {
                    return RedirectToAction("Index", "Admin");//跳转到管理员的主页
                }
                else
                {
                    return RedirectToAction("Index", "User");//跳转到用户的主页
                }
            }
            else
            {
                return Content("<script>alert('用户名或密码错误!');local.href='/Home/Index'</script>");
            }
        }
    }
}
View Code

  HomeController里面的entity对象就是专门用来访问数据库的,通过它可以简单方便的对数据库里面的数据表(entity.Users就对应着数据库中的用户表)进行增删查改。

当我看到它的简洁方便之处时,灵感来了,我就在想,为什么我不用这种以对象的方式来实现那个专门用于访问xml数据文件的动态链接库呢?

  对于为什么要以对象的方式来访问xml数据表就简单介绍到这里,关键是你要动手去开发过,你才知道这种方式的简洁方便之处。

  让我们在(三)中接着详谈怎样以对象的方式来访问xml数据表。

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在Visual Studio 2019中开启预览.. 下一篇运算符

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目