zoukankan      html  css  js  c++  java
  • 用linq实现登陆功能

    BLL层的逻辑代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BLL
    {
        public class Class1
        {
            public bool IsLogin(string name, string pwd,out string msg)
            {
    	    //创建linq对象
                DataClasses1DataContext context = new DataClasses1DataContext();
    	    //得到linq中数据库的表
                var stu = context.student;
                //根据账号得到一个用户名  判断用户是否存在  如果存在则判断密码是否正缺
                //如果不存在则提示用户不存在
                var student = context.student.Where(p => p.username == name).FirstOrDefault();
                if (student == null)
                {
                    msg = "用户不存在";
                    return false;
                }
                else if (student.password != pwd)
                {
                    msg = "密码错误";
                    return false;
                }
                else
                {
                    msg = "登录成功";
                    return true;
                }
            }
        }
    }
    

     页面中的代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                账号:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
                密码:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><br />
                <asp:Label ID="lbShow" runat="server" Text=""></asp:Label>
                <asp:LinkButton ID="lbLogin" runat="server" OnClick="lbLogin_Click">登录</asp:LinkButton>
            </div>
        </form>
    </body>
    </html>
    

     页面的后置代码

    using BLL;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void lbLogin_Click(object sender, EventArgs e)
        {
    	//得到再文本中输入的内容
            string name = this.txtUserName.Text.ToString();
            string pwd = this.txtPwd.Text.ToString();
    	//创建业务逻辑层中类的对象
            Class1 cl = new Class1();
            string msg = "";
    	//判断是否登录成功
            if (cl.IsLogin(name, pwd, out msg))
            {
                this.lbShow.Text = msg;
            }
            else
            {
                Response.Redirect("index.aspx");
            }
        }
    }
    
  • 相关阅读:
    1-Java继承中多态情况特性下变量,方法,静态方法的访问
    3-表约束修改
    2-表结构修改
    持续运维与监控整理
    vscode markdown思维导图插件
    [Python][学习资料]Python学习资料整理
    [高效办公]使用synergy让多台电脑共用一套鼠标键盘
    vim代码片段插件ultisnips的使用
    matplotlib提示RuntimeWarning: Glyph 20998 missing from current font
    vscode使用anaconda的python环境提示“Can't connect to HTTPS URL because the SSL module is not available”
  • 原文地址:https://www.cnblogs.com/liuchang/p/3327877.html
Copyright © 2011-2022 走看看