asp.net连接sql数据库实现信息管理系统

asp.net连接sql数据库实现学生信息管理系统

vs2019连接Sql Server数据库详解
vs2019实现asp.net对SQL Server完整的增删改查

关注公众号并回复:“asp数据库资源 ”即可获取源码
在这里插入图片描述

在登录过程中使用了Cookies记住密码,选择记住密码之后在有效期内再次打开页面是能够实现记住账号密码功能。
Login.aspx
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="index.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="loginBody">
            <h2>学生信息系统登录</h2>
            <br />
            <hr />
            <br />
            <asp:Label ID="Label1" runat="server" Text="账号:" ></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br />
            <asp:Label ID="Label3" runat="server" Text="记住我" Width="50px"></asp:Label>
            <asp:CheckBox ID="CheckBoxRememberMe" runat="server" Width="20px"/>
            <asp:Button ID="Button1" runat="server" Text="登录"  CssClass="btnHover" OnClick="btnclick1"/>
            <asp:Button ID="Button2" runat="server" Text="注册"  CssClass="btnHover" OnClick="btnclick2"/>
            <br />
        </div>
    </form>
</body>
Login.aspx.cs
		protected void Page_Load(object sender, EventArgs e)
        {
            // Cookies判断过程
            if (!IsPostBack)
            {
                if (Request.Cookies["Name"] != null && Request.Cookies["Password"] != null)
                {
                    this.TextBox1.Text = Request.Cookies["Name"].Value;
                    this.TextBox2.Attributes["value"] = Request.Cookies["Password"].Value;
                }
            }
        }

        protected void btnclick1(object sender, EventArgs e)
        {
            // Cookies记住密码过程
            Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
            if (CheckBoxRememberMe.Checked)
            {
                // 如果选中了记住密码,则有效期为7天
                Response.Cookies["Name"].Expires = DateTime.Now.AddDays(7);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7);
            }

            //登录账号判断过程
            Response.Cookies["Name"].Value = this.TextBox1.Text.Trim();
            Response.Cookies["Password"].Value = this.TextBox2.Text.Trim();
            // 如果为空
            if (TextBox1.Text == "" || TextBox2.Text == "")
            {
                Response.Write("<script>alert('用户名或密码不能为空!!')</script>");
            }

            // 如果不为空
            else
            {
                string strcon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;//从web.config文件中读取连接字符串
                SqlConnection con = new SqlConnection(strcon);//定义连接对象
                SqlCommand cmd = new SqlCommand();//创建命令对象
                cmd.Connection = con;//设置命令对象的数据库连接属性
                cmd.CommandText = "select * from studentLogin where username = '" + TextBox1.Text + "' and password = '"+ TextBox2.Text +"'";
                con.Open();//打开数据库连接
                           //Response.Write("连接数据库查询成功");
                SqlDataReader sdr = cmd.ExecuteReader();//执行SQL命令,并获取查询结果
                if (sdr.Read())//如果匹配成功读取数据库内容
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    Response.Write("<script>alert('用户名或密码错误!!')</script>");//否则登陆失败
                }
            }
        }
        protected void btnclick2(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }
Register.aspx
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="index.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="loginBody">
            <h2>账号注册</h2>
            <br />
            <hr />
            <br />
            <asp:Label ID="Label1" runat="server" Text="账号:" ></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="注册"  CssClass="btnHover" OnClick="btnclick1"/>
            <br />
        </div>
    </form>
</body>
Register.aspx.cs
protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnclick1(object sender, EventArgs e)
        {
            var username = this.TextBox1.Text.Trim();
            var password = this.TextBox2.Text.Trim();
            if (username == "" || password == "" )
            {
                Response.Write("<script>alert('账号密码不能为空!')</script>");
                return;
            }

            string strcon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;//从web.config文件中读取连接字符串
            SqlConnection con = new SqlConnection(strcon);//定义连接对象
            SqlCommand cmd = new SqlCommand();//创建命令对象
            cmd.Connection = con;//设置命令对象的数据库连接属性


            // 判断该账号是否已经存在数据库中
            cmd.CommandText = "select * from studentLogin where username = '" + username + "'";
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                Response.Write("<script>alert('该账号已经存在,请重新输入!')</script>");
                con.Close();
            }

            // 如果不存在,则加入
            else
            {
                SqlConnection con1 = new SqlConnection(strcon);//定义连接对象
                SqlCommand cmd1 = new SqlCommand();//创建命令对象
                cmd1.Connection = con1;//设置命令对象的数据库连接属性
                cmd1.CommandText = "insert into studentLogin(username,password) values('" + username + "','" + password + "')";//把SQL语句赋给命令对象

                con1.Open();//打开数据库连接
                sdr = cmd1.ExecuteReader();//执行SQL命令,并获取查询结果
                con1.Close();
                con1.Open();//打开数据库连接
                cmd1.Connection = con1;//设置命令对象的数据库连接属性
                cmd1.CommandText = "select * from studentLogin";//把SQL语句赋给命令对象
                sdr = cmd1.ExecuteReader();//执行SQL命令,并获取查询结果
                con1.Close();//关闭数据库连接
                Response.Redirect("Login.aspx");
                //SqlDataReader sdr = cmd.executeUpdate();
            }
        }
用户登录账号信息数据表

在这里插入图片描述
在这里插入图片描述

登录页面

在这里插入图片描述

注册页面

在这里插入图片描述#### 登录账号判断
在这里插入图片描述

数据增删改查页面

在这里插入图片描述

index.css
body {
    background-color: #8CC5FF;
}

.loginBody {
    position: fixed;
    left: 50%;
    top: 50%;
    width: 250px;
    padding: 40px;
    background: rgba(0,0,0,.5);
    border-radius: 10px;
    box-shadow: 5px 5px 30px rgba(0,0,0,.5);
    transform: translate(-50%,-50%);
    animation: fadein ease-in-out .5s forwards;
}

    .loginBody h2 {
        text-align: center;
    }

@keyframes fadein {
    0% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}

.btnHover {
    cursor: pointer;
}

    .btnHover:hover {
        font-weight: 900;
        border: 2px solid #aaa;
        box-shadow: 2px 2px 10px rgba(0,0,0,.2)
    }

.loginBody input {
    outline: none;
    width: 90%;
    font-size: 18px;
    box-sizing: border-box;
    padding: 5px 10px;
    margin: 10px;
    border-radius: 5px;
    border: 2px solid #eee;
    transition: all .3s;
    background: #eee;
    color: #666;
    font-weight: 400;
}

    .loginBody input:focus {
        border: 2px solid #aaa;
    }

源码过多,需要可私信哦
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小灰灰学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值