版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MirabelleZWH/article/details/84335604
【查】
1.返回布尔值,不需要知道表里的内容,只判断是否存在
UI层
Entity.UserInfo entity = new Entity.UserInfo();
entity.UserID = txtUserID.Text;
Facade.LoginFacade facade = new Facade.LoginFacade();
bool flag = facade.selectUser(entity);
if (flag != false)
{
MessageBox.Show("成功!");
}
else
{
MessageBox.Show("失败!");
}
门面层
public class LoginFacade
{
public bool selectUser(Entity.UserInfo user)
{
BLL.LoginBLL bll = new BLL.LoginBLL();
bool flag = new bool();
flag = bll.selectUser(user);
return flag;
}
}
BLL层
public class LoginBLL
{
public bool selectUser(Entity.UserInfo user)
{
Factory.LoginFactory factory = new Factory.LoginFactory();//实例化工厂
IDAL.LoginIDAL idal = factory.selectUser();//调用factory方法创建接口
DataTable table = idal.selectUser(user);//接收D层返回值
bool flag = new bool();
if (table.Rows.Count == 0)
{
flag = false;
}
else
{
flag = true;
}
return flag;
}
}
工厂层
public class LoginFactory
{
//接收来自配置文件的数据
string strDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
//登录用户
public IDAL.LoginIDAL selectUser()
{
string ClassName = strDB + "." + "LoginDAL";//D层类名
return (IDAL.LoginIDAL)Assembly.Load(strDB).CreateInstance(ClassName);
}
}
IDAL层
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo user);
}
DAL层
public class LoginDAL : IDAL.LoginIDAL
{
public DataTable selectUser(Entity.UserInfo user)
{
SqlHelper sqlhelper = new SqlHelper();
SqlParameter[] sqlParams ={
new SqlParameter("@UserID",user.UserID)
};
string sql = @"SELECT * FROM userInfo WHERE UserID=@UserID";
DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}
2.返回的只一张表,要用表里的内容
UI层
Entity.StuInfo stu = new Entity.StuInfo();
stu.UserID = lblUserID.Text.Trim();
Facade.StuFacade facade = new Facade.StuFacade();
DataTable table = facade.selectStu(stu);
lblUserName.Text = table.Rows[0][1].ToString();
lblSex.Text=table.Rows[0][2].ToString();
门面层
public class StuFacade
{
public DataTable selectStu(Entity.StuInfo stu)
{
StuBLL stuBLL = new StuBLL();
DataTable table = stuBLL.selectStuBLL(stu);
return table;
}
}
BLL层
public class StuBLL
{
public DataTable selectStuBLL (Entity.StuInfo stu)
{
Factory.LoginFactory factory = new Factory.LoginFactory();
IDAL.StuIDAL idal = factory.Student();
DataTable table = idal.selectStu(stu);
return table;
}
}
工厂层
public class LoginFactory
{
//接收来自配置文件的数据
string strDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.StuIDAL Student()
{
string ClassName = strDB + "." + "StuDAL";
return (IDAL.StuIDAL)Assembly.Load(strDB).CreateInstance(ClassName);
}
}
IDAL层
public interface StuIDAL
{
DataTable selectStu(Entity.StuInfo stu);
}
DAL层
public class StuDAL:IDAL.StuIDAL
{
public DataTable selectStu(Entity.StuInfo stu)
{
SqlHelper sqlHelper = new SqlHelper();
SqlParameter[] sqlParams ={
new SqlParameter("@UserID",stu.UserID)
};
string sql = @"SELECT * FROM studentInfo WHERE UserID=@UserID";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}