各位大侠,小弟最近才接触三层架构,遇到个疑问。BLL: public D_BL.BLCollection GetAllBL(M_BL1 bl1)
       {
           return new D_BL().GetAllBL(bl1);
       }DAL:public BLCollection GetAllBL(M_BL1 bl1)
        {
            BLCollection bls = new BLCollection();
            string sql = "Select t.pnl_id,t.prod_nbr,t.cust_nbr,t.bl_log_mach,t.work_ctr,t.bl_factory,t.reason,t.position,t.attribute,t.user_id,to_char(t.key_date,'yyyy-MM-dd hh24:mi:ss') as key_date  From bl_bad t";            sql += " Where fac_id='"+bl1.fac_id+"'";
            if (bl1.date1 != "")
            {
                sql += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";
            }
            if (bl1.date2 != "")
            {
                sql += " and to_char(key_date,'yyyy-MM-dd')<='" + bl1.date2 + "'";
            }
            if (bl1.cust_nbr != "")
            {
                sql += " and cust_nbr='" + bl1.cust_nbr + "'";
            }
            
            
            OracleDataReader dr = null;
            try
            {
                dr = Get_DataReader(sql);
                while (dr.Read())
                {
                    bls.Add(PopuplateBL(dr));
                }
            }
            catch (OracleException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (dr != null && !dr.IsClosed)
                {
                    dr.Close();
                }
                conn.Close();
            }
            return bls;
        }我在写SQL语句的时候,把这些判断逻辑写到了DAL,如果想写进BLL层怎么写呢。
        if (bl1.date1 != "")
            {
                sql += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";
            }
            if (bl1.date2 != "")
            {
                sql += " and to_char(key_date,'yyyy-MM-dd')<='" + bl1.date2 + "'";
            }
            if (bl1.cust_nbr != "")
            {
                sql += " and cust_nbr='" + bl1.cust_nbr + "'";
            }

解决方案 »

  1.   

    sql语句不能往BLL里写,你只能在DAL中写好,在BLL中调用!
      

  2.   

    你可以不要BLL层,把DAL直接在UI调用。
      

  3.   

    三层只是个代名词,你可以写成5层,7层。但重要的是业务逻辑层,UI层只负责与界面交互就行了,而把所有逻辑交给BLL,DAL只负责与数据库打交道,你这明显的是两头粗,中间细,实际上应该是中间粗,两头细。
      

  4.   

    拼SQL的逻辑可以写在BLL层数据访问层直接获取一句SQL就可以了
      

  5.   

    BLL: public D_BL.BLCollection GetAllBL(M_BL1 bl1)
       {
       string sqlWhere="
    sqlWhere Where fac_id='"+bl1.fac_id+"'";
      if (bl1.date1 != "")
      {
      sqlWhere += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";
      }
      if (bl1.date2 != "")
      {
      sqlWhere += " and to_char(key_date,'yyyy-MM-dd')<='" + bl1.date2 + "'";
      }
      if (bl1.cust_nbr != "")
      {
      sqlWhere += " and cust_nbr='" + bl1.cust_nbr + "'";
      }
        "
      return new D_BL().GetAllBL(bl1);
      }DAL:public BLCollection GetAllBL(M_BL1 bl1,string sqlWhere)
      {
      BLCollection bls = new BLCollection();
      string sql = "Select t.pnl_id,t.prod_nbr,t.cust_nbr,t.bl_log_mach,t.work_ctr,t.bl_factory,t.reason,t.position,t.attribute,t.user_id,to_char(t.key_date,'yyyy-MM-dd hh24:mi:ss') as key_date From bl_bad t";
      sql+=sqlwhere;
        
      OracleDataReader dr = null;
      try
      {
      dr = Get_DataReader(sql);
      while (dr.Read())
      {
      bls.Add(PopuplateBL(dr));
      }
      }
      catch (OracleException ex)
      {
      throw new Exception(ex.Message);
      }
      finally
      {
      if (dr != null && !dr.IsClosed)
      {
      dr.Close();
      }
      conn.Close();
      }
      return bls;
      }
     
      

  6.   

    我之前看三层的说明,是说应尽量把所有的业务逻辑都写到BLL。现在我是在做一个查询报表,像这种情况,UI有多个参数传递过来,而参数的值又直接影响到SQL语句写法。
    这种情况是不是只能在DAL判定SQL语句了?
      

  7.   

    控制层 也就属于ASPX.CS文件 应该写应用逻辑(显示方面一些逻辑) 相当于MVC中的CONTROLLER
    逻辑层 处理业务逻辑
    数据层 数据访问操作
      

  8.   

    拼SQL的语句执行方法在DAL 我感觉这样最好 代码清晰 事务处理也是写最逻辑层最好