entity
users1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
/// <summary>
/// 实体层,用户实体类
/// </summary>
public class Users1
{
//字段
private int _userId;
private string _userName;
private string _password1;
private DateTime _regDate;
private int _loginCount;
private DateTime _lastLoginDate;
private string _passwordQuestion;
private string _passwordAnswer;
private bool _isForbidden;
/// <summary>
/// 用户编号
/// </summary>
public int UserId
{
get { return this._userId; }
set { this._userId = value; }
}
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return this._userName; }
set { this._userName = value; }
}
/// <summary>
/// 登录密码
/// </summary>
public string Password1
{
get { return this._password1; }
set { this._password1 = value; }
}
/// <summary>
/// 注册时间
/// </summary>
public DateTime RegDate
{
get { return this._regDate; }
set { this._regDate = value; }
}
/// <summary>
/// 登录次数
/// </summary>
public int LoginCount
{
get { return this._loginCount; }
set { this._loginCount = value; }
}
/// <summary>
/// 最后登录时间
/// </summary>
public DateTime LastLoginDate
{
get { return this._lastLoginDate; }
set { this._lastLoginDate = value; }
}
/// <summary>
/// 找回密码提示问题
/// </summary>
public string PasswordQuestion
{
get { return this._passwordQuestion; }
set { this._passwordQuestion = value; }
}
/// <summary>
/// 找回密码答案
/// </summary>
public string PasswordAnswer
{
get { return this._passwordAnswer;}
set { this._passwordAnswer = value;}
}
/// <summary>
/// 是否被禁止登录
/// </summary>
public bool IsForbidden
{
get { return this._isForbidden; }
set { this._isForbidden = value; }
}
}
}
DAL
SqlDbHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
/// <summary>
/// asp.net 三层架构
/// 针对sql server数据库操作的通用类
/// 作者:虾米大王
/// 日期:2021年6月1日
/// 版本: 1.0
/// </summary>
public class SqlDbHelper
{
private string _connectionString;
/// <summary>
/// 构造函数
/// </summary>
public SqlDbHelper()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="connectionString">数据库的连接字符串</param>
public SqlDbHelper(string connectionString)
{
this._connectionString = connectionString;
}
/// <summary>
/// 设置数据库连接字符串
/// </summary>
public string ConnectionString
{
set
{
this._connectionString = value;
}
}
/// <summary>
/// 执行查询,返回DataTable结果集
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <param name="parameters">执行语句的参数数组</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable ExecuteDataTable(string sql, System.Data.CommandType cmdType, System.Data.SqlClient.SqlParameter[] parameters)
{
System.Data.DataTable table1 = new System.Data.DataTable();
//使用using,是为了包含其中的代码执行结束后自动关闭
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(this._connectionString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
cmd.CommandType = cmdType;
if (parameters != null)
{
foreach (System.Data.SqlClient.SqlParameter para in parameters)
{
cmd.Parameters.Add(para);
}
}
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
adapter.Fill(table1);
}
}
return table1;
}
/// <summary>
/// 执行查询,返回DataTable结果集(函数同名,重构)
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable ExecuteDataTable(string sql)
{
return ExecuteDataTable(sql, System.Data.CommandType.Text, null);
}
/// <summary>
/// 执行查询,返回DataTable结果集(函数同名,重构)
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable ExecuteDataTable(string sql, System.Data.CommandType cmdType)
{
return ExecuteDataTable(sql, cmdType, null);
}
/// <summary>
/// 执行查询,返回一个SqlDataReader的对象实例
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <param name="parameters">参数数组</param>
/// <returns>SqlDataReader对象实例</returns>
public System.Data.SqlClient.SqlDataReader ExecuteReader(string sql, System.Data.CommandType cmdType, System.Data.SqlClient.SqlParameter[] parameters)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(this._connectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con);
if (parameters != null)
{
foreach (System.Data.SqlClient.SqlParameter para in parameters)
{
cmd.Parameters.Add(para);
}
}
con.Open();
//执行行为,关闭reader,同时自动关闭con
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 执行查询,返回一个SqlDataReader的对象实例(函数同名,重构)
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <returns>SqlDataReader对象实例</returns>
public System.Data.SqlClient.SqlDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, System.Data.CommandType.Text, null);
}
/// <summary>
/// 执行查询,返回一个SqlDataReader的对象实例(函数同名,重构)
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">SqlDataReader对象实例</param>
/// <returns>SqlDataReader对象实例</returns>
public System.Data.SqlClient.SqlDataReader ExecuteReader(string sql, System.Data.CommandType cmdType)
{
return ExecuteReader(sql, cmdType, null);
}
/// <summary>
/// 执行查询,返回结果集为首行首列的值
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <param name="parameters">参数数组</param>
/// <returns>首行首列的值</returns>
public Object ExecuteScalar(string sql, System.Data.CommandType cmdType, System.Data.SqlClient.SqlParameter[] parameters)
{
object result = null;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(this._connectionString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
cmd.CommandType = cmdType;
if (parameters != null)
{
foreach (System.Data.SqlClient.SqlParameter para in parameters)
{
cmd.Parameters.Add(para);
}
}
con.Open();
result = cmd.ExecuteScalar();
}
}
return result;
}
/// <summary>
/// 执行查询,返回结果集为首行首列的值
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <returns>首行首列的值</returns>
public Object ExecuteScalar(string sql)
{
return ExecuteScalar(sql, System.Data.CommandType.Text, null);
}
/// <summary>
/// 执行查询,返回结果集为首行首列的值
/// </summary>
/// <param name="sql">要执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <returns>首行首列的值</returns>
public Object ExecuteScalar(string sql, System.Data.CommandType cmdType)
{
return ExecuteScalar(sql, cmdType, null);
}
/// <summary>
/// 对数据库执行增删改操作
/// </summary>
/// <param name="sql">执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <param name="parameters">参数数组</param>
/// <returns>受影响的行数</returns>
public int ExecuteNonQuery(string sql, System.Data.CommandType cmdType, System.Data.SqlClient.SqlParameter[] parameters)
{
int count = 0;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(this._connectionString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
cmd.CommandType = cmdType;
if (parameters != null)
{
foreach (System.Data.SqlClient.SqlParameter para in parameters)
{
cmd.Parameters.Add(para);
}
}
con.Open();
count = cmd.ExecuteNonQuery();
}
}
//执行insert,update,delete之后,返回的受影响行数
return count;
}
/// <summary>
/// 对数据库执行增删改操作(函数同名,重构)
/// </summary>
/// <param name="sql">执行的SQL语句</param>
/// <returns>受影响的行数</returns>
public int ExecuteNonQuery(string sql)
{
return ExecuteNonQuery(sql, System.Data.CommandType.Text, null);
}
/// <summary>
/// 对数据库执行增删改操作(函数同名,重构)
/// </summary>
/// <param name="sql">执行的SQL语句</param>
/// <param name="cmdType">执行语句的类型,可以是存储过程或者SQL文本</param>
/// <returns>受影响的行数</returns>
public int ExecuteNonQuery(string sql, System.Data.CommandType cmdType)
{
return ExecuteNonQuery(sql, cmdType, null);
}
/// <summary>
/// 返回当前数据库中,由用户创建的所有表
/// </summary>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable GetTables()
{
System.Data.DataTable table1 = null;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(this._connectionString))
{
con.Open();
table1 = con.GetSchema("Tables");
}
return table1;
}
}
}
users1DAL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace DAL
{
/// <summary>
/// 操作层,针对Users1表操作的DAL
/// </summary>
public class Users1DAL
{
/// <summary>
/// 数据库连接字符串
/// </summary>
private string _connectionString;
/// <summary>
/// 构造函数
/// </summary>
public Users1DAL()
{
this._connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ToString();
}
/// <summary>
/// 添加用户
/// </summary>
/// <param name="info">用户实体类</param>
/// <returns>布尔值,true是成功</returns>
public bool AddUser(Entity.Users1 info)
{
string sql = "insert into Users1(username,password1,passwordquestion,passwordanswer) values(@uName,@pwd,@question,@answer)";
System.Data.SqlClient.SqlParameter[] parameters = new System.Data.SqlClient.SqlParameter[4];
parameters[0] = new System.Data.SqlClient.SqlParameter("@uName",System.Data.SqlDbType.NVarChar,30);
parameters[0].Value = info.UserName;
parameters[1] = new System.Data.SqlClient.SqlParameter("@pwd", System.Data.SqlDbType.NVarChar, 50);
parameters[1].Value = info.Password1;
parameters[2] = new System.Data.SqlClient.SqlParameter("@question", System.Data.SqlDbType.NVarChar, 30);
parameters[2].Value = info.PasswordQuestion;
parameters[3] = new System.Data.SqlClient.SqlParameter("@answer", System.Data.SqlDbType.NVarChar, 30);
parameters[3].Value = info.PasswordAnswer;
SqlDbHelper helper = new SqlDbHelper(this._connectionString);
int count = helper.ExecuteNonQuery(sql, System.Data.CommandType.Text,parameters);
if (count > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="userid">用户编号</param>
/// <returns>布尔值,true是成功</returns>
public bool DeleteUser(int userid)
{
string sql = string.Format("delete from Users1 where userId={0}",userid);
return new SqlDbHelper(this._connectionString).ExecuteNonQuery(sql) > 0;
}
/// <summary>
/// 更新用户
/// </summary>
/// <param name="info">用户实体类</param>
/// <returns>布尔值,true是成功</returns>
public bool UpdateUser(Entity.Users1 info)
{
//本系统中,一旦注册成功后,是不允许修改用户名和注册时间的。
string sql = "update Users1 set password1=@pwd,logincount=@count,lastlogindate=@lastlogindate,passwordquestion=@question,passwordanswer=@answer,isforbidden=@isforbidden where userId=@uId";
System.Data.SqlClient.SqlParameter[] parameters = new System.Data.SqlClient.SqlParameter[7];
parameters[0] = new System.Data.SqlClient.SqlParameter("@pwd",System.Data.SqlDbType.NVarChar,50);
parameters[0].Value = info.Password1 ;
parameters[1] = new System.Data.SqlClient.SqlParameter("@count",System.Data.SqlDbType.Int,4);
parameters[1].Value = info.LoginCount ;
parameters[2] = new System.Data.SqlClient.SqlParameter("@lastlogindate",System.Data.SqlDbType.DateTime,8);
parameters[2].Value = info.LastLoginDate ;
parameters[3] = new System.Data.SqlClient.SqlParameter("@question",System.Data.SqlDbType.NVarChar,30);
parameters[3].Value = info.PasswordQuestion ;
parameters[4] = new System.Data.SqlClient.SqlParameter("@answer",System.Data.SqlDbType.NVarChar,30);
parameters[4].Value = info.PasswordAnswer ;
parameters[5] = new System.Data.SqlClient.SqlParameter("@isforbidden ",System.Data.SqlDbType.Bit,1);
parameters[5].Value = info.IsForbidden ? 1 : 0;
parameters[6] = new System.Data.SqlClient.SqlParameter("@uId",System.Data.SqlDbType.Int,4);
parameters[6].Value = info.UserId ;
return new SqlDbHelper(this._connectionString).ExecuteNonQuery(sql, System.Data.CommandType.Text, parameters) > 0;
}
/// <summary>
/// 根据用户编号或者用户名查询用户信息
/// </summary>
/// <param name="userid">用户编号</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable GetUser(int userid)
{
string sql = string.Format("select * from Users1 where userId={0}",userid);
return new SqlDbHelper(this._connectionString).ExecuteDataTable(sql);
}
/// <summary>
/// 根据用户编号或者用户名查询用户信息
/// </summary>
/// <param name="username">用户名</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable GetUser(string username)
{
string sql = "select * from Users1 where username=@uName";
System.Data.SqlClient.SqlParameter[] paras = new System.Data.SqlClient.SqlParameter[1];
paras[0] = new System.Data.SqlClient.SqlParameter();
paras[0].ParameterName = "@uName";
paras[0].SqlDbType = System.Data.SqlDbType.NVarChar;
paras[0].SqlValue = 30;
paras[0].Value = username;
return new SqlDbHelper(this._connectionString).ExecuteDataTable(sql, System.Data.CommandType.Text, paras);
}
/// <summary>
/// 按照用户编号升序查询从指定位置开始的指定条数的记录
/// </summary>
/// <param name="startIndex">开始位置</param>
/// <param name="size">指定条数</param>
/// <returns>DataTable结果集</returns>
public System.Data.DataTable GetUserList(int startIndex, int size)
{
string sql = string.Format("select top {0} * from Users1 where userId not in ( select top {1} userId from Users1 order by userId asc) orader by userId asc",size,startIndex);
return new SqlDbHelper(this._connectionString).ExecuteDataTable(sql);
}
/// <summary>
/// 查询用户总数
/// </summary>
/// <returns></returns>
public int GetUserCount()
{
string sql = "select count(*) from Users1";
return int.Parse(new SqlDbHelper(this._connectionString).ExecuteScalar(sql).ToString());
}
}
}
BLL
users1BLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
/// <summary>
/// 逻辑层
/// </summary>
public class Users1BLL
{
/// <summary>
/// 添加用户
/// </summary>
/// <param name="info">用户实体类</param>
/// <returns>布尔值</returns>
public static bool AddUser(Entity.Users1 info)
{
DAL.Users1DAL uDal = new DAL.Users1DAL();
System.Data.DataTable table1 = uDal.GetUser(info.UserName);
if (table1.Rows.Count > 0)
{
return false;
}
else
{
info.Password1 = MakeMD5(info.Password1);
return new DAL.Users1DAL().AddUser(info);
}
}
/// <summary>
/// 对字符串进行MD5加密
/// </summary>
/// <param name="pwd"></param>
/// <returns></returns>
private static string MakeMD5(string pwd)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] sourceBytes = System.Text.Encoding.UTF8.GetBytes(pwd);
byte[] resultBytes = md5.ComputeHash(sourceBytes);
StringBuilder buffer = new StringBuilder(resultBytes.Length);
foreach (byte b in resultBytes)
{
buffer.Append(b.ToString("x"));
}
return buffer.ToString();
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="userid">用户编号</param>
/// <returns>布尔值</returns>
public static bool DeleteUser(int userid)
{
return new DAL.Users1DAL().DeleteUser(userid);
}
/// <summary>
/// 更新用户
/// </summary>
/// <param name="info">用户实体类</param>
/// <param name="changePwd">是否对密码加密</param>
/// <returns>布尔值</returns>
public static bool UpdateUser(Entity.Users1 info, bool changePwd)
{
if (changePwd == true)
{
info.Password1 = MakeMD5(info.Password1);
}
return new DAL.Users1DAL().UpdateUser(info);
}
/// <summary>
/// 根据用户名或用户编号查询用户
/// </summary>
/// <param name="userid">用户编号</param>
/// <returns>DataTable结果集</returns>
public static System.Data.DataTable GetUser(int userid)
{
return new DAL.Users1DAL().GetUser(userid);
}
/// <summary>
/// 根据用户名或用户编号查询用户
/// </summary>
/// <param name="username"></param>
/// <returns>DataTable结果集</returns>
public static System.Data.DataTable GetUserName(string username)
{
return new DAL.Users1DAL().GetUser(username);
}
/// <summary>
/// 按照用户编号升序查询执行位置开始的指定条数的记录
/// </summary>
/// <param name="start">开始位置</param>
/// <param name="size">指定条数</param>
/// <returns>DataTable结果集</returns>
public static System.Data.DataTable GetUserList(int start, int size)
{
return new DAL.Users1DAL().GetUserList(start, size);
}
/// <summary>
/// 查询用户总数
/// </summary>
/// <returns>返回数值</returns>
public static int GetUserCount()
{
return new DAL.Users1DAL().GetUserCount();
}
/// <summary>
/// 用户登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static bool Login(string userName, string password)
{
bool exists = false;
Entity.Users1 info = GetUserEntity(userName);
if (info != null && MakeMD5(password) == info.Password1 && !info.IsForbidden)
{
exists = true;
info.LoginCount = info.LoginCount + 1;
info.LastLoginDate = DateTime.Now;
UpdateUser(info, false);
}
return exists;
}
/// <summary>
/// 根据用户名或者用户编号从数据库中查询用户
/// </summary>
/// <param name="userid">用户编号</param>
/// <returns></returns>
public static Entity.Users1 GetUserEntity(int userid)
{
return ChangeToEntity(new DAL.Users1DAL().GetUser(userid));
}
/// <summary>
/// 根据用户名或者用户编号从数据库中查询用户
/// </summary>
/// <param name="username">用户名</param>
/// <returns></returns>
public static Entity.Users1 GetUserEntity(string username)
{
return ChangeToEntity(new DAL.Users1DAL().GetUser(username));
}
/// <summary>
/// 将包含Users1表记录的DataTable中的第1条记录转换成用户实例
/// </summary>
/// <param name="table1">包含Users1表记录的DataTale</param>
/// <returns></returns>
private static Entity.Users1 ChangeToEntity(System.Data.DataTable table1)
{
Entity.Users1 info = null;
if (table1 != null && table1.Rows.Count > 0)
{
System.Data.DataRow row = table1.Rows[0];
info = new Entity.Users1();
info.UserId = int.Parse(row["userId "].ToString());
info.UserName = row["username"].ToString();
info.Password1 = row["password1"].ToString();
info.RegDate = DateTime.Parse(row["regdate"].ToString());
info.LoginCount = int.Parse(row["logincount"].ToString());
info.LastLoginDate = DateTime.Parse(row["lastlogindate"].ToString());
info.PasswordQuestion = row["passwordquestion"].ToString();
info.PasswordAnswer = row["passwordanswer"].ToString();
info.IsForbidden = bool.Parse(row["isforbidden"].ToString());
}
return info;
}
}
}
work065
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work065.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>使用三层架构模式,模拟用户注册的示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr>
<td>用户名</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="必填" ControlToValidate="txtUserName" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="3-15字符或数字" ControlToValidate="txtUserName" ValidationExpression="\w{3,15}" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>密码</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="必填" ControlToValidate="txtPassword" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="3-15个字符或数字" ControlToValidate="txtPassword" ValidationExpression="\w{3,15}" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>确认密码</td>
<td>
<asp:TextBox ID="txtCPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="必填" ControlToValidate="txtCPassword" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="两次密码不一致" ControlToValidate="txtCPassword" ControlToCompare="txtPassword" Display="Dynamic"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>找回密码提示问题</td>
<td>
<asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="必填" ControlToValidate="txtQuestion" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>找回密码提示答案</td>
<td>
<asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="必填" ControlToValidate="txtAnswer" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnRegister" runat="server" Text="注册" OnClick="btnRegister_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work065
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
protected void btnRegister_Click(object sender, EventArgs e)
{
Entity.Users1 user01 = new Entity.Users1();
user01.UserName = txtUserName.Text;
user01.Password1 = txtPassword.Text;
user01.PasswordQuestion = txtQuestion.Text;
user01.PasswordAnswer = txtAnswer.Text;
if(BLL.Users1BLL.AddUser(user01))
{
Response.Write("<script>javascript:alert('注册成功!');</script>");
}
else
{
Response.Write("<script>javascript:alert('注册失败!');</script>");
}
ClearText();
}
private void ClearText()
{
txtUserName.Text = string.Empty;
txtQuestion.Text = string.Empty;
txtAnswer.Text = string.Empty;
}
}
}
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="connect" connectionString="Data Source=(local);Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=123456;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
sql
create table Users1(
userId int identity(1,1) not null,
username nvarchar(30) not null,
password1 nvarchar(50) not null,
regdate datetime null default getdate(),
logincount int null default 1,
lastlogindate datetime null default getdate(),
passwordquestion nvarchar(30) not null,
passwordanswer nvarchar(30) not null,
isforbidden bit null default 0,
primary key(userId),
unique(username)
)
源代码请去我上传的资源中找一下。