登录页.aspx.cs

来自「简易学生信息管理系统」· CS 代码 · 共 57 行

CS
57
字号
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class 登录页 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if ((this.TextBox1.Text == "") || (this.TextBox2.Text == ""))
        {
            Label1.Text = "用户名与密码不能为空";
        }
        else
        {
            SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=sang;database=student"); //创建数据库连接
            con.Open();//打开连接
            string strsql = "select 密码 from tb2 where 用户名='" + this.TextBox1.Text + "'";//从表中提取密码
            SqlCommand cmd = new SqlCommand(strsql, con);//创建SqlCommand的对象cmd;
            DataSet ds = new DataSet();//创建名为ds的数据集
            SqlDataAdapter da = new SqlDataAdapter(strsql, con);//创建数据适配器 da
            da.Fill(ds,"mytable");//调用da的Fill方法
            try
            {
                if (this.TextBox2.Text == ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())//验证密码是否正确
                {
                   // string curuser = this.TextBox1.Text;
                    Response.Redirect("Default.aspx");//如果用户名和密码无误,就成功登录
                }
                else
                {
                    Label1.Text = "用户名或密码错误";
                }

            }
            catch
            {
                Label1.Text = "你输入的用户名不存在!";
            }
            con.Close();
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?