七层结构
UI:通过界面展示给用户信息,并将输入的信息传递到下一层执行操作。 Entity层(实体层):构建一个对象需要实例化Entity层,这个对象可以把UI界面中的信息存储在这个对象中,通过对象进行信息传递。Facade(外观层):负责整合B层的方法,为一些共有的方法提供统一的接口,以供UI层调用(外观模式),也是对B层的抽象。
BLL(业务逻辑层):执行业务逻辑操作,需要访问数据库的时候交给下一层。 Factory(工厂):可以随便的更换数据库不用更改代码,直接更改配置文件,使用工厂+反射技术,功能抽象成工厂。
IDAL(接口层):定义统一的接口,解除B和D层的耦合,B层通过调用工厂生产的接口就可以联系D层。
DAL(数据库访问层):具备访问数据库的能力。执行对数据库的操作,增,删,改,查等。
SQL Help:对数据库的复用,将对数据库操作chon重复的部分封装起来,可以减少代码量。
部分代码
UI层
namespace UI
{
private void btnOK_Click(object sender, EventArgs e)
{
try
{
Entity.Userinfo user = new Entity.Userinfo();//实例化Userinfo
user.userID = Convert.ToInt32(txtuserID.Text.Trim());//将字符串类型转换成int类型
user.password = txtpwd.Text.Trim();
Boolean flag = false;//定义给bool型的变量
Facade.LoginFacade Flogin = new Facade.LoginFacade();//实例化外观
flag = Flogin.SelectUser(user);//进入到Facade层,执行LoginFacade的方法,返回flag的bool型值
if (flag != false)
{
this.Hide();//隐藏当前窗体
this.DialogResult = System.Windows.Forms.DialogResult.OK;//实现界面跳转
frmMain frmMain = new frmMain();//实例化一个窗体
frmMain.Show();//显示实例化的窗体
}
else
{
MessageBox.Show("密码或者用户名错误,请重新输入!");
txtpwd.Clear();
txtuserID.Clear();
txtuserID.Focus();
}
}
catch (Exception)
{
MessageBox.Show("用户名或密码不能为空,请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtpwd.Clear();
txtuserID.Clear();
txtuserID.Focus();
}
}
private void btncancel_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);//终止此进程
}
}
}
Entity层
namespace Entity
{
public class Userinfo
{
//定义用户ID
private int _userid;
public int userID
{
get { return _userid; }
set { _userid = 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; }
}
}
}
Facade层
namespace Facade
{
public class LoginFacade
{
public Boolean SelectUser(Entity.Userinfo user)
{
bool flag;
BLL.LoginBLL userBLL = new BLL.LoginBLL();//实例化LoginBLL
flag = userBLL.UserBLL(user);//进入BLL层,并执行操作,返回有用户为true,返回无用户为False
return flag;//返回flag
}
}
}
BLL层
namespace BLL
{
public class LoginBLL
{
public bool UserBLL(Entity.Userinfo userinfo)
{
Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
IDAL.LoginIDAL idal = fact.CreateUser();//进入Factory层,调用工厂创建接口
DataTable table = idal.selectUser(userinfo);//接受D层的返回值
bool flag;
//返回值是datatable的类型,如果行数为0,则没有符合该账号及密码的用户
if (table.Rows.Count == 0)
{
flag = false;
}
else
{
flag = true;
}
return flag;//返回flag
}
}
}
Factory层
namespace 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);//反射的应用
}
}
}
IDAL层
namespace IDAL
{
public interface LoginIDAL
{
//DataTable表示内存中数据的一个类
DataTable selectUser(Entity.Userinfo Userinfo);//查找用户
}
}
DAL层
namespace DAL
{
public class LoginDAL:IDAL.LoginIDAL
{
public DataTable selectUser(Entity.Userinfo UserInfo)//查找用户
{
SQLHelper.LoginSQLHelper sqlhelper = new SQLHelper.LoginSQLHelper();
SqlParameter[] sqlParams = { new SqlParameter("@userid", UserInfo.userID), new SqlParameter("@Password", UserInfo.password) };//传参
string sql = "SELECT * FROM User_Info WHERE userID=@userid AND PWD=@Password";//查询语句
DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;//返回一个table类型的结果
}
}
}
SQLHelper层
namespace SQLHelper
{
public class LoginSQLHelper
{
private SqlConnection conn = null;//初始化为空
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
public LoginSQLHelper()
{
string connstr = ConfigurationManager.AppSettings["connstr"];//提供对客户端应用程序配置文件的访问
conn = new SqlConnection(connstr);
}
private SqlConnection Getconn()//判断sqlconnection连接是否关闭
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
public DataTable ExecuteQuery(string cmdText,SqlParameter[]paras,CommandType ct)//执行带参数的查询SQL语句
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, Getconn());//定义其具有的两个方法对数据库的连接及进行查询
cmd.CommandType = ct;//定义sql语句的类型
//不用Add方法,用多个参数同时添加方法AddRange
//封装SQLParameter一次性添加一行sql参数
cmd.Parameters.AddRange(paras);
//方法将SQL语句发送给SqlConnection并生产一个SqlDataReader类对象,该SqlDataReader对象包含SQL命令返回的数据
//执行里面代码块后,关闭sdr
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);//将从数据源获得的DataReader值填充到DataTable
}
return dt;//返回一个表
}
}
}