Web用户管理之用户注册
首先,在MSSQL数据库(DataBase)中,创建一个表(Table),结构如下:
| 列名(Column) | 数据类型(DataType) | 长度(Length) | 备注 | 
| id | uniqueidentifier | 16 | 主码(PrimaryKey) | 
| username | nvartext | 20 | |
| userpwd | nvartext | 20 | 
其次,我们选择 md5算法 对用户的密码进行加密。代码(Code)如下:
此页面为WebControl
RegUser.ascx
 <%@ Control Language="c#" AutoEventWireup="false" Codebehind="RegUser.ascx.cs"
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="RegUser.ascx.cs"Inherits="WebApplication1.control.RegUser"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<LINK href="../style/css.css" type="text/css" rel="stylesheet">
<form id="form_reguser" method="post" runat="server">
 <table id="table2" cellpadding="0" width="600" align="center" border="0">
 <table id="table2" cellpadding="0" width="600" align="center" border="0"> <tr>
   <tr> <td width="160" align="left" valign="top">
     <td width="160" align="left" valign="top"> <TABLE id="Table_sub1" cellSpacing="0" cellPadding="0" width="140" border="0">
       <TABLE id="Table_sub1" cellSpacing="0" cellPadding="0" width="140" border="0"> <TBODY>
         <TBODY> <TR>
           <TR><td>
 <table id="table1" cellSpacing="0" cellPadding="0" width="600" align="center" border="0">
              <table id="table1" cellSpacing="0" cellPadding="0" width="600" align="center" border="0"> <tr>
               <tr> <td class="contexttitle" vAlign="bottom" align="center" height="100">
                 <td class="contexttitle" vAlign="bottom" align="center" height="100">注册用户<span id="Message" runat="server"></span></td>
 </tr>
               </tr> <tr>
               <tr> <td>
                 <td> <table cellSpacing="1" cellPadding="1" width="100%" align="center" border="0">
                   <table cellSpacing="1" cellPadding="1" width="100%" align="center" border="0"> <tr>
                     <tr> <td align="center">用户名
                       <td align="center">用户名 <asp:textbox id="TextBoxUser" MaxLength="16" Runat="server"></asp:textbox>
                         <asp:textbox id="TextBoxUser" MaxLength="16" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="请填写"
ControlToValidate="TextBoxUser">
</asp:requiredfieldvalidator>
</td>
 </tr>
                     </tr> <tr>
                     <tr> <td align="center"><font color="red">密码初始值与用户名相同<br>
                       <td align="center"><font color="red">密码初始值与用户名相同<br> 请及时修改您的密码</font></td>
                         请及时修改您的密码</font></td> </tr>
                     </tr> <tr>
                     <tr> <td align="center"><asp:Button ID="Buttonsubmit" Runat="server"
                       <td align="center"><asp:Button ID="Buttonsubmit" Runat="server"Text="注册" Width="100"></asp:Button></td>
 </tr>
                     </tr> </table>
                   </table> </td>
                 </td> </tr>
                </tr> </table>
             </table> </td>
           </td> </tr>
        </tr> </table>
    </table> </form>
</form>
Reguser.ascx.cs
 namespace WebApplication1.control
namespace WebApplication1.control {
{ using System;
    using System; using System.Data;
    using System.Data; using System.Drawing;
    using System.Drawing; using System.Web;
    using System.Web; using System.Web.UI.WebControls;
    using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
    using System.Web.UI.HtmlControls; using System.Data.SqlClient;
    using System.Data.SqlClient; using System.Text;
    using System.Text;
 /// <summary>
    /// <summary> ///        RegUser 的摘要说明。
    ///        RegUser 的摘要说明。 /// </summary>
    /// </summary> public class RegUser : System.Web.UI.UserControl
    public class RegUser : System.Web.UI.UserControl {
    { protected System.Web.UI.WebControls.Button Buttonsubmit;
        protected System.Web.UI.WebControls.Button Buttonsubmit; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
        protected System.Web.UI.HtmlControls.HtmlGenericControl Message; protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Data.SqlClient.SqlCommand sqlCommand1;
        protected System.Data.SqlClient.SqlCommand sqlCommand1; protected System.Web.UI.WebControls.TextBox TextBoxUser;
        protected System.Web.UI.WebControls.TextBox TextBoxUser;
 private void Page_Load(object sender, System.EventArgs e)
        private void Page_Load(object sender, System.EventArgs e) {
        { // 在此处放置用户代码以初始化页面
            // 在此处放置用户代码以初始化页面 if(!Page.IsPostBack)
            if(!Page.IsPostBack) {
            { Message.InnerHtml="";
                Message.InnerHtml=""; }
            } }
        }
 Web 窗体设计器生成的代码
        Web 窗体设计器生成的代码
 private void Buttonsubmit_Click(object sender, System.EventArgs e)
        private void Buttonsubmit_Click(object sender, System.EventArgs e) {
        { string strUser=this.TextBoxUser.Text.Trim();
            string strUser=this.TextBoxUser.Text.Trim(); this.sqlCommand1.CommandText="select * from tbuser where username='"+strUser+"'";
            this.sqlCommand1.CommandText="select * from tbuser where username='"+strUser+"'"; try
            try {
            { this.sqlConnection1.Open();
                this.sqlConnection1.Open(); SqlDataReader dr=this.sqlCommand1.ExecuteReader();
                SqlDataReader dr=this.sqlCommand1.ExecuteReader(); if(dr.HasRows)
                if(dr.HasRows) {
                { this.Message.InnerHtml=
                    this.Message.InnerHtml="<br><font size='4' color='red'>该用户名已经存在<br>"
+"请重新注册</font>";
 }
                } else
                else {
                { this.sqlConnection1.Close();
                    this.sqlConnection1.Close(); Encoding ascii=Encoding.ASCII;
                    Encoding ascii=Encoding.ASCII; byte[] data=ascii.GetBytes(strUser);
                    byte[] data=ascii.GetBytes(strUser); System.Security.Cryptography.MD5 md5=
                    System.Security.Cryptography.MD5 md5=new System.Security.Cryptography.MD5CryptoServiceProvider();
 byte[] result=md5.ComputeHash(data);
                    byte[] result=md5.ComputeHash(data); string pwdmd5=Convert.ToBase64String(result);
                    string pwdmd5=Convert.ToBase64String(result); string cmdText="Insert Into tbuser(username,userpwd) values('"+
                    string cmdText="Insert Into tbuser(username,userpwd) values('"+ strUser+"','"+pwdmd5+"')";
                        strUser+"','"+pwdmd5+"')"; SqlCommand sqlCommand2=new SqlCommand(cmdText,this.sqlConnection1);
                    SqlCommand sqlCommand2=new SqlCommand(cmdText,this.sqlConnection1); this.sqlConnection1.Open();
                    this.sqlConnection1.Open(); sqlCommand2.ExecuteNonQuery();
                    sqlCommand2.ExecuteNonQuery(); sqlCommand2.Dispose();
                    sqlCommand2.Dispose(); this.Message.InnerHtml="<br><font size='4' color='blue'>注册成功,"
                    this.Message.InnerHtml="<br><font size='4' color='blue'>注册成功,"+"请尽快修改密码</font>";
 }
                } }
            } catch(SqlException Ex)
            catch(SqlException Ex) {
            { this.Message.InnerHtml="<br><font size='4' color='red'>错误"
                this.Message.InnerHtml="<br><font size='4' color='red'>错误"+Ex.Message+"</font>";
 }
            } finally
            finally {
            { this.sqlCommand1.Dispose();
                this.sqlCommand1.Dispose(); this.sqlConnection1.Close();
                this.sqlConnection1.Close(); }
            } }
        } }
    } }
}
 
 
                    
                     
                    
                 
                    
                

 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号