前周和本周主要学习登录和注册的内容,其中登录和注册分别有三种方法。登录可以通过查询标量、参数、参数方向三种命令来实现;注册有写入、异常、存储过程三种实现命令。
结合上课内容,我们分别总结和实现这几种命令。
(1)实现登录:
①运用查询标量:
C#主要代码如下:
private void btn_LogIn_Click(object sender, EventArgs e)
{
SqlConnection sqlC = new SqlConnection();
sqlC.ConnectionString ="Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlC;
sqlCmd.CommandText = //指定SQL命令的命令文本;命令文本由字符串拼接而成;
"SELECT COUNT(1) FROM tb_doctor"
+ " WHERE no=‘" + this.txb_no.Text.Trim() + "‘"
+ " AND Password=HASHBYTES(‘MD5‘,‘" + this.txb_password.Text.Trim() + "‘);";
//将文本框的文本清除首尾的空格后,拼接至命令文本中;
sqlC.Open();
int rowC = (int)sqlCmd.ExecuteScalar();//将文本框的文本清除首尾的空格后,拼接至命令文本中; //执行标量的返回结果类型为object,可通过强制类型转换,转为整型;
MessageBox.Show(sqlCmd .CommandText );
sqlC.Close();
if (rowC == 1)
{ MessageBox.Show("登录成功"); }
else
{ MessageBox.Show("工号/密码错误,请重新输入");
this.txb_password.Focus();
this.txb_password.SelectAll();
}
}
运行结果如下图:
②运用参数:
C#的主要代码如下:
private void btn_LogIn_Click(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection();
sql.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlC = sql.CreateCommand();
sqlC.CommandText =
"select count(1) from tb_doctor where [email protected] and password=HASHBYTES(‘MD5‘,@password)"; //指定SQL命令的命令文本;命令文本包含参数
SqlParameter sqlP = //声明SQL参数
sqlC.Parameters.AddWithValue("@no",this.txb_no .Text .Trim ());
//调用方法AddWithValue向SQL命令的参数集合添加参数的名称、值,同时实例化SQL参数;
sqlP.SqlDbType = SqlDbType.Char;//设置SQL参数对应的SQL Server数据类型
sqlP.Size = 10; //设置SQL参数的长度;
sqlC.Parameters.AddWithValue("@password",this .txb_password .Text .Trim ());
//直接调用方法AddWithValue向SQL命令的参数集合添加参数的名称、值;
sqlC.Parameters["@password"].SqlDbType = SqlDbType.VarChar;
//通过参数名称访问SQL参数,并将密码参数的类型设为变长字符串;
//SQL参数能自动识别类型;若参数值为字符串,则类型自动设为NVARCHAR,且可在执行时自动转换;但对于相同密码,VARCHAR/NVARCHAR类型所获得的散列值不同,故需手动将SQL参数类型统一设为VARCHAR;
sql.Open();
int rowC = (int)sqlC.ExecuteScalar();
//调用SQL命令的方法ExecuteScalar来执行命令,并接受单个结果(即标量);
sql.Close();
if (rowC == 1) { MessageBox.Show("登录成功"); }
else { MessageBox.Show("密码错误,重新输入");
this.txb_no.Focus();
this.txb_password.SelectAll();
}
运行结果如下:
③运用参数方向:
C#主要代码如下:
private void btn_LogIn_Click(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection();
sql.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlC = sql.CreateCommand();
sqlC.CommandText = "usp_select_doctorCount";
//指定SQL命令的命令文本;命令文本为存储过程名称;
sqlC.CommandType = CommandType.StoredProcedure;
//SQL命令的类型设为存储过程;
sqlC.Parameters.AddWithValue("@no", this.txb_no.Text.Trim());
//调用方法AddWithValue向SQL命令的参数集合添加参数的名称、值;
sqlC.Parameters.AddWithValue("@password", this.txb_password.Text.Trim());
sqlC.Parameters.Add("@RowCount", SqlDbType.TinyInt);
//调用方法Add向SQL命令的参数集合添加参数的名称、SQL Server数据类型;
sqlC.Parameters["@password"].SqlDbType = SqlDbType.VarChar;
sqlC.Parameters.Add("@LastLogInAddress", SqlDbType.VarChar, 20);
sqlC.Parameters["@RowCount"].Direction = ParameterDirection.Output;
//设置SQL参数方向为输出;
sqlC.Parameters["@LastLogInAddress"].Direction = ParameterDirection.Output;
sql.Open();
sqlC.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据;
sql.Close();
int rowCount = (byte)sqlC.Parameters["@RowCount"].Value; //声明整型变量,用于保存与所输用户号相应的行计数,并从相应的输出参数值中获得行计数;存储过程中该参数类型为TINYINT,需转换为相应的byte类型;
string LastLogInAddress = sqlC.Parameters["@LastLogInAddress"].Value.ToString();
//声明整型变量,用于保存上次登录时间,并从相应的输出参数值中获得上次登录时间;
if (rowCount == 1)
{
MessageBox.Show("登录成功。\n您上次登录地址是" + LastLogInAddress + "。");
}
else
{
MessageBox.Show("用户号/密码有误,请重新输入!");
this.txb_password.Focus();
this.txb_password.SelectAll();
}
SQL代码如下:
use HISDatabase ;
CREATE TABLE tb_LogIn
(
no
char(10)
NOT NULL
PRIMARY KEY
,name
varchar(10)
not null
,sex
bit
not null
,post
varchar(20)
,department
varchar(30)
,phone
varchar(20)
,birthdate
date
,password
varbinary(128)
not null
,LastLogInAddress
VARCHAR(20)
NULL
)
GO
CREATE PROCEDURE usp_select_doctorCount
(
@no char(10)
,@password VARCHAR(20)
,@RowCount TINYINT OUTPUT
,@LastLogInAddress VARCHAR(20) OUTPUT)
AS
BEGIN
DECLARE
@tb_LastLogInAddress TABLE
(LastLogInAddress
VARCHAR(20));
SELECT
@RowCount=0;
UPDATE tb_LogIn
SET
LastLogInAddress=
(SELECT
C.client_net_address
FROM
sys.dm_exec_connections AS C
WHERE
[email protected]@SPID)
OUTPUT deleted.LastLogInAddress
INTO @tb_LastLogInAddress
WHERE
[email protected]
AND Password=HASHBYTES(‘MD5‘,@Password);
SELECT
@RowCount=1
,@LastLogInAddress=L.LastLogInAddress
FROM
@tb_LastLogInAddress AS L
WHERE
@@ROWCOUNT=1;
END
select * from tb_LogIn
INSERT tb_LogIn
(no,name,sex,post,department,phone,birthdate,password,LastLogInAddress)
VALUES
(‘3140707001‘,‘莎莎‘,0,‘经济‘,‘内科‘,‘18259062233‘,‘1992-12-13‘,HASHBYTES(‘MD5‘,‘1213‘),‘192.168.1.10‘);
C#运行结果如下图:
通过以上代码的对比,运用标量命令的方法较为简单,但是要注意,SQL语句不能写错。
(2)实现注册
①写入
C#主要代码如下:
private void btn_SignUp_Click(object sender, EventArgs e)
{
if (this.txb_no.Text.Trim() == "")
{
MessageBox.Show("工号不能为空");
this.txb_no.Focus();
return;
}
if (this.txb_name.Text.Trim() == "")
{
MessageBox.Show("姓名不能为空");
this.txb_name.Focus();
return;
}
if (this.txb_password.Text.Trim() == "")
{
MessageBox.Show("密码不能为空");
this.txb_password.Focus();
return;
}
SqlConnection sql = new SqlConnection();
sql.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlC = sql.CreateCommand();
sqlC.CommandText =
"Insert tb_doctor (no,name,sex,post,department,phone,birthdate,password) values (@no,@name,@sex,@post,@department,@phone,@birthdate,HASHBYTES(‘MD5‘,@password))";
sqlC.Parameters.AddWithValue("@no", this.txb_no.Text.Trim());
sqlC.Parameters.AddWithValue("@name", this.txb_name.Text.Trim());
sqlC.Parameters.AddWithValue("@sex", this.rbt_1.Checked);
sqlC.Parameters.AddWithValue("@post", this.txb_post.Text.Trim());
sqlC.Parameters.AddWithValue("@department", this.txb_department.Text.Trim());
sqlC.Parameters.AddWithValue("@phone", this.txb_phone.Text.Trim());
sqlC.Parameters.AddWithValue("@birthdate", this.txb_birthdate.Text.Trim());
sqlC.Parameters.AddWithValue("@password", this.txb_password.Text.Trim());
sqlC.Parameters["@password"].SqlDbType = SqlDbType.VarChar;
sql.Open();
int rowA = sqlC.ExecuteNonQuery();
sql.Close();
if (rowA ==1)
{
MessageBox.Show("注册成功");
}
else
{
MessageBox.Show("注册失败");
}
}
运行结果如下:
查询得到:
②异常
C#的主要代码如下:
private void btn_SignUp_Click(object sender, EventArgs e)
{
if (this.txb_no.Text.Trim() == "") { MessageBox.Show("工号不能为空");
this.txb_no.Focus();
return;
}
if (this.txb_name.Text.Trim() == "")
{
MessageBox.Show("姓名不能为空");
this.txb_name.Focus();
return;
}
if (this.txb_password.Text.Trim() == "")
{
MessageBox.Show("密码不能为空");
this.txb_password.Focus();
return;
}
SqlConnection sql = new SqlConnection();
sql.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlC = sql.CreateCommand();
sqlC.CommandText =
"Insert tb_doctor (no,name,sex,post,department,phone,birthdate,password) values (@no,@name,@sex,@post,@department,@phone,@birthdate,HASHBYTES(‘MD5‘,@password))";
//指定SQL命令的命令文本;命令文本包含参数
sqlC.Parameters.AddWithValue("@no",this.txb_no .Text .Trim ());
sqlC.Parameters.AddWithValue("@name", this.txb_name.Text.Trim());
sqlC.Parameters.AddWithValue("@sex", this.rbt_1.Checked);
sqlC.Parameters.AddWithValue("@post", this.txb_post.Text.Trim());
sqlC.Parameters.AddWithValue("@department", this.txb_department.Text.Trim());
sqlC.Parameters.AddWithValue("@phone", this.txb_phone.Text.Trim());
sqlC.Parameters.AddWithValue("@birthdate", this.txb_birthdate.Text.Trim());
sqlC.Parameters.AddWithValue("@password", this.txb_password.Text.Trim());
sqlC.Parameters["@password"].SqlDbType = SqlDbType.VarChar;
int rowA = 0;
string message = "";
try
{
sql.Open();
rowA = sqlC.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
if (sqlEx.Number == 2627)
{ message = "已存在,请重新输入"; }
else
{ message = "注册失败"; }
}
finally
{ sql.Close(); }
if (rowA == 1)
{ message = "注册成功"; }
MessageBox.Show(message );
}
运行结果如下:
③存储过程
C#主要代码如下:
private void btn_SignUp_Click(object sender, EventArgs e)
{
if (this.txb_no.Text.Trim() == "")
{
MessageBox.Show("工号不能为空");
this.txb_no.Focus();
return;
}
if (this.txb_name.Text.Trim() == "")
{
MessageBox.Show("姓名不能为空");
this.txb_name.Focus();
return;
}
if (this.txb_password.Text.Trim() == "")
{
MessageBox.Show("密码不能为空");
this.txb_password.Focus();
return;
}
SqlConnection sql = new SqlConnection();
sql.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlC = sql.CreateCommand();
sqlC.CommandText = "usp_Doctor";//指定SQL命令的命令文本;命令文本为存储过程名称;
sqlC.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlC.Parameters.AddWithValue("@no", this.txb_no.Text.Trim());
sqlC.Parameters.AddWithValue("@name", this.txb_name.Text.Trim());
sqlC.Parameters.AddWithValue("@sex", this.rbt_1.Checked);
sqlC.Parameters.AddWithValue("@post", this.txb_post.Text.Trim());
sqlC.Parameters.AddWithValue("@department", this.txb_department.Text.Trim());
sqlC.Parameters.AddWithValue("@phone", this.txb_phone.Text.Trim());
sqlC.Parameters.AddWithValue("@birthdate", this.txb_birthdate.Text.Trim());
sqlC.Parameters.AddWithValue("@password", this.txb_password.Text.Trim());
sql.Open();
int rowA = 0;
try
{
rowA = sqlC.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
if (sqlEx.Number == 2627)
{
MessageBox.Show("您注册的用户号已存在,请重新输入!\n");
}
}
sql.Close();
if (rowA == 1)
{
MessageBox.Show("注册成功。");
}
else
{
MessageBox.Show("注册失败!");
}
}
数据库代码如下
use HISDatabase ;
GO
CREATE PROCEDURE usp_Doctor
(@no char(10)
,@name varchar(10)
,@sex bit
,@post varchar(20)
,@department varchar(30)
,@phone varchar(20)
,@birthdate date
,@password VARCHAR(20))
AS
BEGIN
INSERT tb_doctor
(no,name,sex,post,department,phone,birthdate,password)
VALUES
(@no
,@name
,@sex
,@post
,@department
,@phone
,@birthdate
,HASHBYTES(‘MD5‘,@Password));
END
由以上运行结果可知,异常是在写入的基础之上加入了try{...}catch{...}finally{...}语句,存储过程代码较为简洁。
第三章例子
3.1利用面向对象的类实现登录功能:
C#的窗体主要代码如下:
namespace _3._1登录_面向对象_类
{
public partial class LogIn : Form
{
private User User;
public LogIn()
{
InitializeComponent();
this.User = new User(); //实例化用户,并赋予本窗体的相应属性
}
private void btn_LogIn_Click(object sender, EventArgs e)
{
this.User.no = this.txb_no.Text.Trim();
//将文本框的文本清除首尾的空格后,赋予用户的相应属性;
this.User.password = this.txb_password.Text.Trim();
this.User.LogIn(); //调用用户的方法登录,并将各文本框的文本作为参数;
MessageBox.Show(this .User.Message);
if (!this.User.HasLoggedIn)
{
this.txb_password.Focus();
this.txb_password.SelectAll();
}
}
}
}
C#类的代码如下:
using System.Data;
using System.Data.SqlClient;
using System.Configuration; //包含访问配置文件所需的配置管理器;需事先在本项目的“引用”中添加对System.Configuration的引用
/// 公有类:用户;
class User
{
public string no
{
get;
set;
}
public string password
{
get;
set;
}
public bool HasLoggedIn
{
get;
set;
}
public bool HasSignedUp
{
get;
set;
}
public string Message
{
get;
set;
}
/// 公有方法:登录;
public void LogIn()
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString();
//配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
//调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand.CommandText = "usp_selectUserCount";
//指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand.CommandType = CommandType.StoredProcedure;
//SQL命令的类型设为存储过程;
sqlCommand.Parameters.AddWithValue("@no", this.no);
sqlCommand.Parameters.AddWithValue("@password", this.password);
sqlConnection.Open();
int rowCount = (int)sqlCommand.ExecuteScalar();
//调用SQL命令的方法ExecuteScalar来执行命令,并接受单个结果(即标量)
sqlConnection.Close();
if (rowCount == 1)
{
this.HasLoggedIn = true;
this.Message = "登录成功。";
}
else
{
this.HasLoggedIn = false;
this.Message = "用户号/密码有误,请重新输入!";
}
}
public void SignUp()
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "usp_insertUser";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@No", this.no);
sqlCommand.Parameters.AddWithValue("@Password", this.password);
sqlConnection.Open();
int rowAffected = 0;
try
{
rowAffected = sqlCommand.ExecuteNonQuery();
//调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
}
catch (SqlException sqlEx)
{
if (sqlEx.Number == 2627)
{
this.Message = "您注册的用户号已存在,请重新输入!\n";
}
}
sqlConnection.Close();
if (rowAffected == 1)
{
this.HasSignedUp = true;
this.Message = "注册成功。";
}
else
{
this.HasSignedUp = false;
this.Message += "注册失败!";
}
}
}
示例如下:
注意事项:创建类的时候在项里添加新建项即可。
3.2面向对象的分层方法实现登录、注册功能:
C#主要代码如下:
登录:
public partial class frm_SignUp : Form
{
/// <summary>
/// 私有属性:用户;
/// </summary>
private User User
{
get;
set;
}
/// <summary>
/// 公有方法:构造函数;
/// </summary>
public frm_SignUp()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen; //本窗体启动位置设为屏幕中央;
this.User = new User(); //实例化用户,并赋予本窗体的相应属性;
}
/// <summary>
/// 私有方法:点击注册按钮;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_SignUp_Click(object sender, EventArgs e)
{
if (this.txb_UserNo.Text.Trim() == "") //若用户号文本框为空;
{
MessageBox.Show("用户号不能为空!"); //给出错误提示;
this.txb_UserNo.Focus(); //用户号文本框获得焦点;
return; //返回;
}
if (this.txb_Password.Text.Trim() == "") //若密码文本框为空;
{
MessageBox.Show("密码不能为空!"); //给出错误提示;
this.txb_Password.Focus(); //密码文本框获得焦点;
return; //返回;
}
this.User.No = this.txb_UserNo.Text.Trim(); //将文本框的文本清除首尾的空格后,赋予用户的相应属性;
this.User.Password = this.txb_Password.Text.Trim();
UserBll.SignUp(this.User); //调用业务逻辑层的静态方法,对用户执行注册操作;
MessageBox.Show(this.User.Message); //在消息框中显示注册消息;
}
}
注册:
public partial class frm_LogIn : Form
{
/// <summary>
/// 私有属性:用户;
/// </summary>
private User User
{
get;
set;
}
/// <summary>
/// 公有方法:构造函数;
/// </summary>
public frm_LogIn()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen; //本窗体启动位置设为屏幕中央;
this.User = new User(); //实例化用户,并赋予本窗体的相应属性;
}
/// <summary>
/// 私有方法:点击登录按钮;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Login_Click(object sender, EventArgs e)
{
this.User.No = this.txb_UserNo.Text.Trim(); //将文本框的文本清除首尾的空格后,赋予用户的相应属性;
this.User.Password = this.txb_Password.Text.Trim();
UserBll.LogIn(this.User); //调用业务逻辑层的静态方法,对用户执行登录操作;
MessageBox.Show(this.User.Message); //在消息框中显示登录消息;
if (!this.User.HasLoggedIn) //若用户未完成登录,即登录失败;
{
this.txb_Password.Focus(); //密码文本框获得焦点;此时将触发事件,并再次执行验证用户号的方法,从而将用户的消息覆盖为验证用户号的结果,故需事先在消息框中显示登录消息;
this.txb_Password.SelectAll(); //密码文本框内所有文本被选中;
}
}
/// <summary>
/// 私有方法:点击注册按钮;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_SignUp_Click(object sender, EventArgs e)
{
frm_SignUp signUpForm = new frm_SignUp(); //声明并实例化注册窗体;
signUpForm.ShowDialog(this); //注册窗体以对话框方式打开;
}
}
用户类:
/// 公有静态类:用户(数据访问层);
public static int SelectCount(User user)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand1 = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand1.CommandText = "usp_selectUserCount"; //指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand1.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlCommand1.Parameters.AddWithValue("@No", user.No); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand1.Parameters.AddWithValue("@Password", user.Password);
sqlConnection.Open(); //打开SQL连接;
int userCount = (int)sqlCommand1.ExecuteScalar(); //调用SQL命令的方法ExecuteScalar来执行命令,并接受单个结果(即标量);
sqlConnection.Close(); //关闭SQL连接;
return userCount; //返回用户个数;
}
/// <summary>
/// 公有静态方法:插入;
/// </summary>
/// <param name="user">用户</param>
/// <returns>受影响行数</returns>
public static int Insert(User user)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand.CommandText = "usp_insertUser"; //指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlCommand.Parameters.AddWithValue("@No", user.No); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@Password", user.Password);
sqlConnection.Open(); //打开SQL连接;
int rowAffected = 0; //声明整型变量,用于保存受影响行数
try //尝试;
{
rowAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
}
catch (SqlException sqlEx) //捕捉SQL异常;
{
if (sqlEx.Number == 2627) //若异常的编号为2627,则违反实体完整性,即插入了主键重复的记录;
{
user.IsDuplicate = true; //存在雷同用户;
}
else
{
throw sqlEx;
}
}
return rowAffected; //返回受影响行数;
}
/// <summary>
/// 公有静态类:用户(业务逻辑层);
/// </summary>
public static class UserBll
{
/// <summary>
/// 公有静态方法:登录;
/// </summary>
/// <param name="user">用户</param>
/// <returns>是否登录成功</returns>
public static bool LogIn(User user)
{
if (UserDal.SelectCount(user) == 1) //调用数据访问层的静态方法,查询与输入的用户号、密码匹配的记录行数;
{ //若匹配行数为1;
user.HasLoggedIn = true; //完成登录;
user.Message = "登录成功。"; //给出正确提示;
}
else //否则;
{
user.HasLoggedIn = false; //未完成登录;
user.Message = "用户号/密码有误,请重新输入!"; //给出错误提示;
}
return user.HasLoggedIn; //返回用户是否登录成功;
}
/// <summary>
/// 公有静态方法:注册;
/// </summary>
/// <param name="user">用户</param>
/// <returns>是否注册成功</returns>
public static bool SignUp(User user)
{
int rowAffected1 = UserDal.Insert(user); //调用数据访问层的静态方法来插入用户,并返回受影响行数;
if (rowAffected1 == 1) //若受影响的行数为1;
{
user.HasSignedUp = true; //完成注册;
user.Message = "注册成功。"; //给出正确提示;
}
else //否则;
{
user.HasSignedUp = false; //未完成注册;
if (user.IsDuplicate) //若用户雷同;
{
user.Message = "您注册的用户号已存在,请重新输入!\n"; //给出错误提示;
}
user.Message += "注册失败!"; //给出错误提示;
}
return user.HasSignedUp; //返回用户是否注册成功;
}
}