csla.net 框架的实例应用二(集合多条记录的演示)

//集合多条记录的演示

窗体调用代码:

 #region 多条数据

 static TcouponList ListCoupon3;

//获取
        TcouponList.Criteria cd=new TcouponList.Criteria();
        cd.MemberIdno = "M020697";
        
        ListCoupon3 = TcouponList.GetCouonList(cd);
        GridView1.DataSource = ListCoupon3;
        GridView1.DataBind();

//增加,更改

TcouponList ListCoupon = TcouponList.NewTcouponList();
        Tcoupon coupon = Tcoupon.NewCoupon();
        coupon.MemberIdNo = "sssss";
        coupon.CouponValue = 20;
        coupon.GenDate = DateTime.Today;
        coupon.Deadline = DateTime.Today;
        coupon.IsUsed = false;
        coupon.Remark = "shite";
        ListCoupon.Add(coupon);
        ListCoupon.Save();

        foreach (Tcoupon cc in ListCoupon3)
        {
            cc.CouponValue = 42;
        }
        ListCoupon3.Save();

        #endregion

业务集合类:

using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using Csla.Data;
using System.Data.SqlClient;

namespace dllcsla
{
    [Serializable]
   public class TcouponList :BusinessListBase<TcouponList,Tcoupon>
    {
        #region Factory Method
        public static TcouponList GetCouonList(Criteria crit)
        {
            return DataPortal.Fetch<TcouponList>(crit);
        }

        public static TcouponList NewTcouponList()
        {
            return new TcouponList();
        }
       
        #endregion

#region Constructors
        private TcouponList()
        {

        }
#endregion
        #region Class
        [Serializable]
      
        public class Criteria
        {
            public string CouponNum;
            public string MemberIdno;
            public DateTime? MaxDeadline;
            public bool IsOnlyGetAvail = false;
            public string IssueHotelName = string.Empty;
        }

        #endregion

        #region SQLDAL
        protected override void DataPortal_Fetch(object criteria)
        {
            Criteria Tcriteria = criteria as Criteria;
            StringBuilder sql = new StringBuilder();
            sql.Append("select a.* from table_coupon a ");

            sql.Append(" where '1'='1'");
            string Ttemp = string.Empty;
            Ttemp = GetReturnString(OperaTeype.dayu, Tcriteria.MaxDeadline, "a.deadline", 1);
            sql.Append(Ttemp);

            Ttemp = GetReturnString(OperaTeype.dengyu, Tcriteria.MemberIdno, "a.memberidno", 1);
            sql.Append(Ttemp);
            SafeDataReader dr = new SafeDataReader(SqlHelper.ExecuteReader(sql.ToString()));
            while(dr.Read())
            {
                this.Add(Tcoupon.GetCoupon(dr));
            }
            dr.Close();

        }

       protected override void DataPortal_Update()
       {

           SqlConnection cn = new SqlConnection("Data Source=.;User ID=sa;Password=123456;Initial Catalog=tblcustom;");
            cn.Open();
           foreach (Tcoupon coupon in this)
           {
               coupon.Update(cn);
           }
           cn.Close();
       }
        #endregion

        enum OperaTeype { dengyu,dayu, xiaoyu, dayuOrdengyu, xiaoyuOrdengyu, inType, like };
        
        #region UtilMethod
        private string GetReturnString(OperaTeype type, object value,string filed,int yihao)
        {
            string strStart=" and ";
            if (value == null)
            {
                return string.Empty;
            }
            if (value.ToString().Length <= 0)
            {
                return string.Empty;
            }
            if (yihao == 1)
            {
                value = "'" + value + "'";
            }
           
            if ((type == OperaTeype.dayu))
            {             
                    return string.Format("{0}{1}>{2}", strStart, filed, value);
                
            }
            if ((type == OperaTeype.dengyu))
            {
                return string.Format("{0}{1}={2}", strStart, filed, value);
            }
            if ((type == OperaTeype.xiaoyu))
            {
                return string.Format("{0}{1}<{2}", strStart, filed, value);
            }
            if ((type == OperaTeype.dayuOrdengyu))
            {
                return string.Format("{0}{1}>={2}", strStart, filed, value);
            }
            if ((type == OperaTeype.xiaoyuOrdengyu))
            {
                return string.Format("{0}{1}<={2}", strStart, filed, value);
            }
            if ((type == OperaTeype.inType))
            {
                return string.Format("{0}{1} in ({2})", strStart, filed, value);
            }
            if ((type == OperaTeype.like))
            {
                return string.Format("{0}{1} like '%{2}%'", strStart, filed, value);
            }
            return string.Empty;
        }
        #endregion
    }
}


   本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

原文地址:https://www.cnblogs.com/wzg0319/p/1777380.html