• Asp.net用户登陆数据库验证与注册写入数据库


    1.思路与效果图

    Index.aspx

    2014-04-08_211903

    注册

    2014-04-08_211933

    注册成功

    2014-04-08_212137

    登陆

    2014-04-08_211956

    登陆验证通过进入内容页1

    2014-04-08_212234

    登陆没通过验证

    2014-04-08_212313

    思路:首先建一个Sqlserver数据库Student,再建一个student表(name,pwd)存放用户名和密码。

    然后注册功能的实现:通过数据库插入信息到表的Sql语句来实现,成功提示用户名和密码。

    登陆验证的实现:查询student表的数据,SqlDataReader取出数据库的数据,一个If判断语句如果符合取得的数据写入session并跳转到内容页1,否则提示错误。

    2.注册功能的实现

    前台:注册页面.aspx

    <asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
        <fieldset class="register">
            <legend>帐户信息</legend>
            <p>
                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
            </p>
            <p>
                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label>
                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
            </p>
        </fieldset>
        <p class="submitButton">
            <asp:Button ID="CreateUserButton" runat="server" Text="创建用户" OnClick="CreateUserButton_Click"/>
        </p>

    后台:

    首先建一个test.cs

    public class DBSimple
        {
    
    
    
            private SqlConnection con;
    
            public DBSimple()
            {
                string str = @"Data Source=PC01;Integrated Security=SSPI;database=Student";
                con = new SqlConnection(str);
    
            }
    
            public void TestExecuteNonQuery_Insert(string name, string pwd)
            {
                if (con == null) return;
                string sql = "insert student values('" + name + "','" + pwd + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                if (con.State == ConnectionState.Closed)
                    con.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
    
                }
            }
    
    
        }

    注册页面.aspx.cs

    DBSimple db;
            protected void Page_Load(object sender, EventArgs e)
            {
                db = new DBSimple();
    
            }
            protected void CreateUserButton_Click(object sender, EventArgs e)
            {
                string name = UserName.Text;
                string pwd = Password.Text;
    
                db.TestExecuteNonQuery_Insert(name, pwd);
    
                Response.Write("<script>alert('Name=" + name + " , pwd =" + pwd + "  ')</script>");                    
    
            }

    3.登陆功能实现

    前台:

    <fieldset class="login">
                <legend>帐户信息</legend>
                <p>
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                </p>
                <p>
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label>
                    <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                </p>
                <p>
                    <asp:CheckBox ID="RememberMe" runat="server"/>
                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">保持登录状态</asp:Label>
                </p>
        </fieldset>
        <p class="submitButton">
            <asp:Button ID="LoginButton" runat="server" Text="登录" OnClick="LoginButton_Click"/>
        </p>

    后台

    protected void LoginButton_Click(object sender, EventArgs e)
            {
                string name = UserName.Text;
                string pwd = Password.Text;
                       
                
                string str = @"Data Source=PC01;Integrated Security=SSPI;database=Student";
                SqlConnection sqlCon = new SqlConnection(str);
                sqlCon.Open();
    
    
                SqlCommand sqlComGet = new SqlCommand();
                sqlComGet.Connection = sqlCon;
    
                sqlComGet.CommandText = "select * from student where name='" + name + "' and pwd='" + pwd + "'";
                SqlDataReader sqlDr = sqlComGet.ExecuteReader();
    
                if (sqlDr.Read())                                  //帐号和密码正确
                {
                    Session["name"] = name;                      //用Session记录帐号
    
                    Session["pwd"] = pwd;                //用Session记录密码
    
                    Response.Redirect("内容页1.aspx");
                }
                else                                              //帐号或密码错误
                {
    
    
                    Response.Write("<script>window.alert('您输入的用户名或密码不正确!');</script>");
    
    
                }
    
             
    
                sqlCon.Close();
    
            
          
    
    
    
            }
  • 相关阅读:
    学password学一定得学程序
    具体解释站点沙盒期的原因表现与解决的方法
    POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)
    [Apple开发者帐户帮助]九、参考(5)支持的功能(tvOS)
    [Apple开发者帐户帮助]九、参考(4)支持的功能(macOS)
    [Apple开发者帐户帮助]九、参考(3)支持的功能(iOS)
    [Apple开发者帐户帮助]九、参考(2)撤销特权
    [Apple开发者帐户帮助]九、参考(1)证书类型
    [Apple开发者帐户帮助]八、管理档案(4)
    [Apple开发者帐户帮助]八、管理档案(3)创建App Store配置文件
  • 原文地址:https://www.cnblogs.com/Energy240/p/3653012.html
Copyright © 2020-2023  润新知