1. 程式人生 > >【機房重構】——增刪改查(四)

【機房重構】——增刪改查(四)

【查】

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 [email protected]";
            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 [email protected]";
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }
    }