【ADO.NET基础-Regidter】简单的账户注册界面和源代码(可用于简单面试基础学习用)

在阅读时如有问题或者建议,欢迎指出和提问,我也是初学者.........

前台代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
 <title></title> 
</head> 
<body> 
 <div align="center"> 
 <h1>用户登录页面</h1> 
 <form id="form1" runat="server"> 
  <p> <asp:Label ID="lbusername" runat="server">学号:</asp:Label> 
   <asp:TextBox ID="txtNum" runat="server"></asp:TextBox> 
   </p> 
     <p> <asp:Label ID="lblName" runat="server">姓名:</asp:Label> 
   <asp:TextBox ID="txtName" runat="server"></asp:TextBox> 
   </p> 
  
  <p> <asp:Label ID="lbpsw" runat="server">密 码:</asp:Label> 
  <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox> 
  </p> 
 
      <p>
         <asp:Label ID="lblText" runat="server" Text=""></asp:Label></p>
  <p> <asp:Button ID="btnRegister" runat="server" Text="注册" onclick="btnRegister_Click" /> 
  </p> 
    
 </form> 
 </div> 
</body> 
</html>

后台代码:

using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Text;
using System.Security.Cryptography;


namespace ado.netDemo1
{
    public partial class register : System.Web.UI.Page
    {
        SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
        string sql;
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (txtNum.Text.Trim() == "" || txtName.Text.Trim() == "" || txtpwd.Text.Trim() == "")
            {
                lblText.ForeColor = Color.Red;
                lblText.Text = "请将个人信息填写完整!";
            }
            else if (txtNum.Text.Trim() == "" && txtName.Text.Trim() == "" && txtpwd.Text.Trim() == "")
            {
                lblText.ForeColor = Color.Red;
                lblText.Text = "请将个人信息填写完整!";
            }
            else
            {   
                //加密处理注册Password
                string hashedpwd=Encrypt(txtpwd.Text.Trim());
                sql = "insert into tb_Students(SID,Name,Password)Values('" + txtNum.Text.Trim() + "','" + txtName.Text.Trim() + "','" + hashedpwd + "')";
                connStr.Open();
                SqlCommand cmd = new SqlCommand(sql, connStr);
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    lblText.ForeColor = Color.Red;
                    lblText.Text = "注册成功!";
                    Response.Write(@"<script>window.alert('请到登陆界面登陆!');window.location='Login.aspx'</script>");
                }
            }
        }

        public static string Encrypt(string cleanString)
        {
            Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
            Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);        
            return BitConverter.ToString(hashedBytes);
            
        }
    }
}

 

界面截图:

 

 

posted @ 2017-07-06 17:15  OldELeven  阅读(320)  评论(0编辑  收藏  举报