【UML】
【过程】
【解释】
- UI层实例化Entity,把窗体的信息传入Entity(举例子:123);实例化Facade,把刚刚的“123”传入Facade中。
- Facade层实例化BLL,把“123”传入BLL的方法里
- BLL层实例化Factory
- Factory层配置文件
- BLL层将Factory的方法的值赋值给IDAL
- Factory层建立IDAL的方法并反射;BLL层把数据“123”付给表
- DAL层实现了IDAL中方法实例化sqlHelper
- DAL层查询数据
- DAL层查询结果返回
- BLL层判断是否有数据,有则去F
- Factory层判断是否有数据,有则去U
- UI层判断是否有数据,有则成功,没有失败。
简单解释:UI把窗体信息放入E里,把值传给Facade(赋值给flag),调用Facade的方法,然后实例化B并调用了B方法(建立另一个flag),然后实例化Factory接收配置文件,用IDAL调用了Factory的方法让其查数据库(D),将返回值传回来,返回去做判断。
【代码】
Entity层
namespace LoginEnitity
{
public class UserInfo
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}
UI层
namespace LoginUI
{
public partial class Form1 : Form
{
private void btnOK_Click(object sender, EventArgs e)
{
LoginEnitity.UserInfo enitity = new LoginEnitity.UserInfo();
//实例化实体层,接收信息
enitity.UserName = txtUserName.Text.Trim();
enitity.Password = txtPassword.Text;
LoginFacade.Facade facade =new LoginFacade.Facade();
//实例化外观层,接收B层返回来的信息
bool flag = new bool();
flag = facade.SelectUser(enitity);
if (flag!=false)
{
if (true)
{
this.Hide();
MessageBox.Show("登陆成功");
}
}
else
{
MessageBox.Show("用户或密码错误,请重新输入。");
txtPassword.Clear();
txtUserName.Clear();
txtUserName.Focus();
}
}
}
}
Facade层
namespace LoginFacade
{
public class Facade
{
public Boolean SelectUser (LoginEnitity.UserInfo enuser)
{
bool flag;
LoginBLL.BLL bll = new LoginBLL.BLL();//实例化B层对象
flag = bll.UserBLL(enuser);
return flag;
}
}
}
BLL层
using System.Data;
namespace LoginBLL
{
public class BLL
{
public bool UserBLL(LoginEnitity.UserInfo UserInfo)
{
LoginFactory.Factory factory = new LoginFactory.Factory();//实例化工厂
LoginIDAL.IDAL idal = factory.CreateUser();//调用工厂方法创建接口
DataTable tabel = idal.GetUserName(UserInfo);//接收D层的返回值
bool flag = new bool();
if (tabel.Rows.Count == 0)
//返回的是datatable的类型, 如果行数等于0,说明没有该账号
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
}
Factory层
using System.Reflection;
using System.Configuration;
namespace LoginFactory
{
public class Factory
{
string strDB =System.Configuration.ConfigurationManager.AppSettings["DB"];
//接受配置文件
public LoginIDAL.IDAL CreateUser()
{
string ClassName = strDB + "." + "DAL";//LoginDAL层的类名
return (LoginIDAL.IDAL)Assembly.Load(strDB).CreateInstance(ClassName);
//反射加工厂的应用
}
}
}
IDAL层
using System.Data;
//表示对ADO.NET结构类的访问,通过ADO.NET,可以生成有效管理多个数据源的数据的组件
namespace LoginIDAL
{
public interface IDAL
{
DataTable GetUserName(LoginEnitity.UserInfo Userinfo);
//DataTable表示内存中数据的一个表。实现了增删改查中 的查
}
}
DAL层
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace LoginDAL
{
public class DAL:LoginIDAL.IDAL //实现接口的方法
{
public DataTable GetUserName(LoginEnitity.UserInfo Userinfo)
{
SqlHelper sqlhelper = new SqlHelper();
SqlParameter[] sqlParams = { new SqlParameter("@UserName", Userinfo.UserName), new SqlParameter("@Password", Userinfo.Password) };
string sql = @"SELECT * FROM Uers WHERE UserName=@UserName AND Password=@Password";
DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;//返回的是一个table的类型
}
}
}
SQLHelper
using System.Data;
using System.Data.SqlClient;
using System.Configuration;//处理配置数据的类型
namespace LoginDAL
{
public class SqlHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;//从数据库中只读
public SqlHelper()
{
string connStr = ConfigurationManager.AppSettings["connStr"];
//Configuration//提供对客户端应用程序配置文件的访问。
conn = new SqlConnection(connStr);
}
private SqlConnection GetConn()//这个是判断数据库的连接
{
if (conn.State==ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
//cmdText查询的文本,getConn是上边的一个sqlconnection 它表示到sql server的连接!
cmd.CommandType = ct;
//他的作用就是如何解释CommandText属性,commandtext就是指的是具体的什么指令
//(sql,或者什么存储过程)
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
//ExecuteReader(CommandBehavior)将CommandText发送到Connection,
//并使用CommandBehavivor值之一生成SqlDataReader
{
dt.Load(sdr); //填充DataTable使用所提供的数据源中的值DataReader
}
return dt; //返回的是一个表
}
}
}
配置文件
<appSettings >
<add key="ConnStr" value ="Server=ZWH;Database=Login;User ID=sa;Password=123456"/>
<!-- Server=数据库名字,Database=表名 -->
<add key="DB" value ="LoginDAL"/>
</appSettings>