zoukankan      html  css  js  c++  java
  • 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();
    
            
          
    
    
    
            }
  • 相关阅读:
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive如何实现音频转码的
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive之跨平台ini配置及通道的方法
    GB/T28181协议EasyGBS播放1080p视频直播会花屏
    国标GB/T28181协议下播放器起播慢或者延迟高如何解决?
    EasyGBS查找大华设备的录像列表时失败
    ffmpeg增加h264编解码功能模块方法
    EasyNVR控制台运行出现invalid license关于计算机保护软件类似于360、腾讯云管家等限制相关问题
    摄像机经过多级路由转换无法被EasyNVR拉流问题处理方法
    使用EasyNVR软件对接海康摄像头对接失败问题解析
    GB/T28181协议使用EasyNVR降低播放延迟方法
  • 原文地址:https://www.cnblogs.com/Energy240/p/3653012.html
Copyright © 2011-2022 走看看