[C \ #] 7 층 로그 인 코드 정리

제 지난 글 에서 7 층 의 개념 적 지식 을 간단하게 설명 해 드 렸 습 니 다. 지금 구체 적 으로 실현 되 는 것 을 보 겠 습 니 다!
B 층
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Factory;
using IDAL;
using System.Data;
namespace 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);

           Boolean falg;
           if (table.Rows.Count == 0)
           {
               falg = false;
           }
           else
           {
               falg = true;
           }
           return falg  ;
       }
    }
}

D
public  class LoginDAL :IDAL.LoginIDAL
    {
      public DataTable selectUser(Entity.UserInfo UserInfo)
      {

          sqlHelper sqlhelper = new sqlHelper(); 
          SqlParameter[] sqlParams = 
          {new SqlParameter("@UserName" , UserInfo.UserName) , 
           new SqlParameter("@PassWord" , UserInfo.PassWord)};
          string sql = @"SELECT * FROM USERS WHERE UserName = @UserName 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;  
        }  


        ///   
        ///            SQL          
        ///   
        ///     SQL  
        ///       
        ///           
        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;  
         }  

        ///   
        ///           SQL         
        ///   
        ///     SQL  
        ///         
        ///       
        ///           
        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;  
        }  
         ///   
        ///           SQL         
        ///   
        ///   SQL         
        ///       
        ///   
        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;  
        }  


        ///   
        ///         SQL         
        ///   
        ///   SQL         
        ///       
        ///       
        ///   
        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;  
        }  
    }  

实体层(Entity)存放全局的实体类,方便各个层之前的参数调用。代码就不都写了!
Facade

public  class LoginFacade
    {
      public Boolean SelectUser(Entity.UserInfo UserInfo)
      {
          bool flag;
          BLL.LoginBLL userBLL = new BLL.LoginBLL();
          flag = userBLL.UserBLL(UserInfo);
          return flag;
      }
    }

Factory
public  class LoginFactory
    {
        //string AssemblyName = "DAL";
        //       ,    (DAL)    
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
        //                 
        public IDAL.LoginIDAL CreateUser()                                              
        {
            string className = StrDB + "." + "LoginDAL";//DAL         

            return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(className);//        
        }
    }

IDAL (인터페이스)
  DataTable selectUser(Entity.UserInfo UserInfo);

UI (메 인 인터페이스 로그 인)
private void btnOK_Click(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text.Trim();
            string PassWord = txtPassWord.Text;

            //  、    
            if (txtUserName.Text == string.Empty)
            {
                MessageBox.Show("     !", "  ");
                return;
            }
            else
            {
                if (txtPassWord.Text == string.Empty)
                {
                    MessageBox.Show("     ", "  ");
                }

                //  
                //try
                //{
                    //      
                    Facade.LoginFacade fLogin = new Facade.LoginFacade();
                    Entity.UserInfo user = new Entity.UserInfo();

                    user.UserName = Convert.ToString(txtUserName.Text.Trim());
                    user.PassWord = Convert.ToString(txtPassWord.Text);


                    Boolean falg = false;
                    falg = fLogin.SelectUser(user);

                    if (falg != false)
                    {
                        MessageBox.Show("    " + txtUserName.Text);
                    }
                    else
                    {
                        MessageBox.Show("    QAQ" + txtUserName.Text);
                    }
            }
        }

코드 부분 은 여기까지 정 리 했 습 니 다. 여러분 께 도움 이 되 셨 으 면 좋 겠 습 니 다!!!!

좋은 웹페이지 즐겨찾기