--用户信息表 CREATE TABLE[dbo].[UserInfo] ( [UserName] [varchar](20)COLLATEChinese_PRC_CI_ASNOTNULL,---用户名 [UserPwd] [varchar](20)COLLATEChinese_PRC_CI_ASNOTNULL,---密码 [Email] [varchar](50)COLLATEChinese_PRC_CI_ASNOTNULL--电子邮件 ) ON[PRIMARY] GO ALTER TABLE[dbo].[UserInfo]ADDCONSTRAINT[PK__UserInfo__C9F2845707020F21]PRIMARYKEYCLUSTERED ([UserName])ON[PRIMARY] GO
/// <summary>
/// 注册用户MODEL
/// </summary>
public class RegisterModel
{
/// <summary>
/// 用户名
/// </summary>
[DisplayName("用户名")]
public string UserName
{ get; set; }
/// <summary>
/// 密码
/// </summary>
[DisplayName("密码")]
public string UserPwd
{
get;
set;
}
[DisplayName("确认密码")]
public string ConfirPwd
{
get;
set;
}
/// <summary>
/// 用户邮箱
/// </summary>
[DisplayName("邮箱")]
public string Email
{
get;
set;
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcLogin.Models.RegisterModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>注册页面</title>
</head>
<body>
<div>
<br />
<p style="font-size:12px;color:red">
<%if (ViewData["msg"] != null)
{%>
<%:ViewData["msg"]%>
<%} %>
</p>
<br />
<%Html.BeginForm(); %>
<table>
<tr>
<td><%: Html.LabelFor(m => m.UserName) %></td>
<td> <%: Html.TextBoxFor(m => m.UserName) %></td>
</tr>
<tr>
<td> <%: Html.LabelFor(m => m.UserPwd) %></td>
<td> <%: Html.PasswordFor(m => m.UserPwd) %></td>
</tr>
<tr>
<td> <%: Html.LabelFor(m => m.ConfirPwd) %></td>
<td> <%: Html.PasswordFor(m => m.ConfirPwd)%></td>
</tr>
<tr>
<td> <%: Html.LabelFor(m => m.Email) %></td>
<td> <%: Html.TextBoxFor(m => m.Email) %></td>
</tr>
<tr>
<td> <input type=submit value="提交" /></td>
<td></td>
</tr>
</table>
<%Html.EndForm(); %>
</div>
</body>
</html>
publicActionResult Register()
{
return View();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace MvcLogin.Models
{
public class SqlHelper
{
//数据库连接字符串
readonly string conStr = "server=.;uid=sa;pwd=123;database=cztest;Min Pool Size=10;";
string strAdd = @"INSERT INTO dbo.UserInfo
( UserName, UserPwd, Email )
VALUES ( '{0}', -- UserName - varchar(20)
'{1}', -- UserPwd - varchar(20)
'{2}' -- Email - varchar(50)
)";
string strUserExist = @" SELECT * FROM dbo.UserInfo WHERE UserName='{0}' AND UserPwd='{1}'";
/// <summary>
/// 添加用户
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool AddUser(RegisterModel model)
{
strAdd = string.Format(strAdd, model.UserName, model.UserPwd, model.Email);
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(strAdd,con);
int o = cmd.ExecuteNonQuery();
con.Close();
if (o>0)
{
return true;
}
return false;
}
/// <summary>
/// 判断用户是否存在
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool ExistUser(RegisterModel model)
{
strUserExist = string.Format(strUserExist, model.UserName, model.UserPwd);
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(strUserExist, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
if (ds!=null&&ds.Tables[0].Rows.Count>0)
{
return true;
}
return false;
}
}
}
/// <summary>
/// 注册提交
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Register(Models.RegisterModel model)
{
bool result = false;
if (!new Models.SqlHelper().ExistUser(model))
{
result = new Models.SqlHelper().AddUser(model);
}
if (result)
{
//添加成功转向主页
FormsService.SignIn(model.UserName, false);
return RedirectToAction("index");
}
else
{
//返回注册页面
ViewData["msg"] = "添加用户失败";
return View(model);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有