版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luckystar_99/article/details/82942595
小激动
经过两天的“刻苦钻研”,终于在今天的9:30分登录成功了!!!其实好久都没有正儿八经的敲代码了,刚开始的时候真是头疼,但是还是硬着头皮开始了重构(因为学习进度真的是慢的不要不要的了!)。
除了感谢自己能够勇敢的往前走了,在登录成功的过程中还要感谢:我桌邵聪、对桌伟杰、还有赵雷童鞋。O(∩_∩)O哈哈~
还有就是每一次做这种小系统,都证明了一个问题:就是真的没自己想的那么难,只要去做就好了,做着做着就懂了,就算不懂,过后就懂了…
七层
-
显示层(UI)
-
外观层(Facade)
实现了外观模式,作为UI和BLL的桥梁,解除了它们之间的耦合 -
逻辑层(BLL)
-
接口层(IDAL)
在这个层次上,定义了一个统一的接口,解除BLL层和DAL层之间的耦合 -
数据访问层(DAL)
在此有拓展一个SQLHelper层,主要使封装对数据库调用的一些代码,这样就提高了我们代码的灵活性! -
工厂层(Factory)
实现了工厂模式+反射,使数据库的修改更加简便,通过修改配置文件实现数据库的修改 -
实体层(Entity)
主要是封装一些功能性代码,定义一些实体类型和实体集合,用于各个层次传递参数
代码实现
1、创建Entity,实现业务实体。
2、创建IDAL,实现接口。
3、创建DAL,实现接口里的方法。
4、增加APP.config里的配置信息,为提供DAL的程序集。
5、创建Factory,返回程序集的指定类的实例。
6、创建BLL,调用Factory,得到程序集指定类的实例,完成数据操作方法。
7、创建Facade,调用BLL,得到BLL层的处理结果返回值。
8、创建UI,调用Facade里的数据操作方法,实现登录。
Entity层
namespace Entity
{
public class UserInfo
{
//定义 用户ID 字段
private int userid;
public int UserID
{
get { return userid; }
set { userid = value; }
}
//定义 用户名 字段
private string username;
public string UserName
{
get { return username; }
set { username = value; }
}
//定义 密码 字段
private string password;
public string PassWord
{
get { return password; }
set { password = value; }
}
//定义 等级 字段
private string level;
public string Level
{
get { return level; }
set { level = value; }
}
//定义 状态 字段
private bool state;
public bool State
{
get { return state; }
set { state = value; }
}
}
IDAL层
namespace IDAL
{
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo UserInfo);
}
}
DAL层
public class LoginDAL : IDAL.LoginIDAL
{
public DataTable selectUser(Entity.UserInfo UserInfo)
{
//实例化一个数据查询对象
SqlHelper sqlHelper = new SqlHelper();
//选择要查询的数据
SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.UserID), new SqlParameter("@PassWord", UserInfo.PassWord) };
//插入语句
string sql = @"SELECT * FROM [User_Info] WHERE userID=@UserID and passWord=@PassWord";
//将参数放在语句中
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
//得到返回值
return table;
}
}
SQLHelper类
public class SqlHelper
{
private SqlConnection conn = null;//打开
private SqlCommand cmd = null;//执行
private SqlDataReader sdr = null;//读取
public SqlHelper()
{
string connStr = ConfigurationManager.AppSettings["connStr"];
conn = new SqlConnection(connStr);
}
private SqlConnection GetConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
/// <summary>
/// 执行不带参数的增删改SQL语句或者存储程序
/// </summary>
/// <param name="cmdText">增删改查SQL</param>
/// <param name="ct">命令的类型</param>
/// <returns>返回受影响的行数</returns>
public int ExecuteNonQuery(string cmdText,CommandType ct)
{
int res;
try
{
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State==ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
/// <summary>
/// 执行带参数的增删改SQL语句或者存储过程
/// </summary>
/// <param name="cmdText">增删改查SQL</param>
/// <param name="paras">要查询的参数</param>
/// <param name="ct">命令类型</param>
/// <returns>返回受影响的行数</returns>
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
/// <summary>
/// 执行不带参数的查询SQL语句或存储过程
/// </summary>
/// <param name="cmdText">查询SQL语句或存储过程</param>
/// <param name="ct">命令类型</param>
/// <returns>返回查询到的行</returns>
public DataTable ExecuteQuery(string cmdText,CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// <summary>
/// 执行带参数的查询SQL语句或存储过程
/// </summary>
/// <param name="cmdText">查询SQL语句或存储过程</param>
/// <param name="paras">参数集合</param>
/// <param name="ct">命令类型</param>
/// <returns>返回查询到的行</returns>
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
}
配置文件:
在APP.config这个文件里添加下面的代码;
<appSettings>
<add key ="ConnStr" value="server=JYJ; database=Jifang;user ID = sa ; pwd=123"/>
<!-- server是自己数据库的名字或者用.代表本地;把database,uid,pwd修改为与自己数据库对应的关系-->
<add key ="DB" value="DAL" />
</appSettings>
Factory层
public class LoginFactory
{
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];//接收来自配置文件的数据
public IDAL.LoginIDAL CreateUser()
{
//DAL层的类名
string ClassName = StrDB + "." + "LoginDAL";
//反射加工厂的应用
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
BLL层
public class LoginBLL
{
public bool UserBLL(Entity.UserInfo UserInfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
IDAL.LoginIDAL idal = fact.CreateUser();//调用工厂方法
DataTable table = idal.selectUser(UserInfo);//接收D层的返回值
bool flag;
if (table.Rows.Count == 0) //返回的DataTable类型,如果它的行数等于0,说明没有符合该账号密码的用户
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
Facade层
public class LoginFacade
{
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();//实例化一个B层
flag = userBLL.UserBLL(user);//接收B层传来的True或者False
return flag;
}
}
UI层
private void btnOK_Click(object sender, EventArgs e)
{
//判断输入不为空
if (txtUserID.Text.Trim()=="")
{
MessageBox.Show("没有输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPassWord.Text=="")
{
MessageBox.Show("没有输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
try
{
//实例化一个用户
Entity.UserInfo user = new Entity.UserInfo();
//接收控件传来的用户名信息
user.UserID = Convert.ToInt32(txtUserID.Text.Trim());
//接收控件传来的密码信息
user.PassWord = txtPassWord.Text;
Boolean flag = false;//定义要给bool的变量
Facade.LoginFacade FLogin = new Facade.LoginFacade();//实例化外观
flag = FLogin.SelectUser(user);//调用外观的方法,返回给user
if (flag!=false)
{
MessageBox.Show("登陆成功!");
//this.Hide();//隐藏当前窗体
//this.DialogResult = System.Windows.Forms.DialogResult.OK;
//FrmMain a = new FrmMain();//实例化一个窗体
//a.Show();//显示实例化窗体
}
else
{
MessageBox.Show("密码或者用户名错误");
}
}
catch (Exception)
{
throw;
}
}
后记
这些代码都是站在巨人的肩膀上敲出来的,但是敲出来并不一定能够运行呦,会出现很多错误,在敲登录时的一些注意事项和错误解决方案,看我的下一篇博客吧!