csla.net 框架的实例应用三(集合多条记录的演示)续
继上页
单条业务类代码:
using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using Csla.Data;
using System.Data.SqlClient;
using System.Data;
namespace dllcsla
{
[Serializable]
public class Tcoupon : BusinessBase<Tcoupon>
{
#region Class Level Private Variables
private int _pkey = -1; //**PK
private string _memberIdno = "";
private int _couponValue = 0;
private DateTime _genDate = DateTime.Today;
private DateTime _deadline = DateTime.Today.AddMonths(12);
private bool _isUsed = false;
private DateTime _useDate = DateTime.MinValue;
private string _remark = "";
private int _issueHotelKey = -1; //Hostel key
#endregion
#region Constructors
private Tcoupon()
{
MarkAsChild();
}
#endregion
#region Business Properties and Methods
public int pkey
{
get { return _pkey; }
}
public string CouponNum { get { return "C" + _pkey.ToString(); } }
public string CouponCombo { get { return "C" + _pkey.ToString() + " : " + _remark + " : " + _couponValue.ToString(); } }
public string MemberIdNo
{
get { return _memberIdno; }
set
{
if (value != _memberIdno)
{
_memberIdno = value;
MarkDirty();
}
}
}
public int CouponValue
{
get { return _couponValue; }
set
{
if (value != _couponValue)
{
_couponValue = value;
MarkDirty();
}
}
}
public DateTime GenDate
{
get { return _genDate; }
set
{
if (value != _genDate)
{
_genDate = value;
MarkDirty();
}
}
}
public DateTime Deadline
{
get { return _deadline; }
set
{
if (value != _deadline)
{
_deadline = value;
MarkDirty();
}
}
}
public bool IsUsed
{
get { return _isUsed; }
set
{
if (value != _isUsed)
{
_isUsed = value;
MarkDirty();
}
}
}
public DateTime UseDate
{
get { return _useDate; }
set
{
if (value != _useDate)
{
_useDate = value;
MarkDirty();
}
}
}
public string UseDateStr { get { return _useDate == DateTime.MinValue ? "" : _useDate.ToShortDateString(); } }
public string Remark
{
get { return _remark; }
set
{
if (value != _remark)
{
_remark = value;
MarkDirty();
}
}
}
public int IssueHotelKey
{
get { return _issueHotelKey; }
set
{
if (value != _issueHotelKey)
{
_issueHotelKey = value;
MarkDirty();
}
}
}
#endregion //Business Properties and Methods
#region System.Object Overrides
protected override object GetIdValue()
{
return _pkey;
}
#endregion //System.Object Overrides
#region Static Methods
public static Tcoupon NewCoupon()
{
return new Tcoupon();
}
public static Tcoupon GetCoupon(SafeDataReader dr )
{
return new Tcoupon(dr);
}
internal Tcoupon(SafeDataReader dr)
{
FetchObject(dr);
MarkAsChild();
}
#endregion
private void FetchObject(SafeDataReader dr)
{
_pkey = dr.GetInt32("pkey");
_couponValue = dr.GetInt32("Value");
_deadline = dr.GetDateTime("Deadline");
_genDate = dr.GetDateTime("GenDate");
_isUsed = dr.GetBoolean("IsUsed");
_memberIdno = dr.GetString("MemberIdno");
_remark = dr.GetString("Remark");
_useDate = dr.GetDateTime("UseDate");
_issueHotelKey = dr.GetInt32("issueHotelKey");
MarkOld();
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Insert()
{
SqlConnection cn = new SqlConnection("Data Source=.;User ID=sa;Password=123456;Initial Catalog=tblcustom;");
cn.Open();
try
{
if (this.IsNew)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "dd_prod_insertcoupon";
AddInsertUpdateParams(cm);
cm.ExecuteNonQuery();
MarkOld();
}
}
}
finally
{
cn.Close();
}
}
private void AddInsertUpdateParams(SqlCommand cm)
{
cm.Parameters.AddWithValue("@MemberIdno", _memberIdno);
cm.Parameters.AddWithValue("@Value", _couponValue);
cm.Parameters.AddWithValue("@GenDate", _genDate);
cm.Parameters.AddWithValue("@Deadline", _deadline);
cm.Parameters.AddWithValue("@isUsed", _isUsed);
if (!this.IsNew)
{
if (_useDate == DateTime.MinValue)
cm.Parameters.AddWithValue("@UseDate", DBNull.Value);
else
cm.Parameters.AddWithValue("@UseDate", _useDate);
}
cm.Parameters.AddWithValue("@Remark", _remark);
cm.Parameters.AddWithValue("@issueHotelKey", _issueHotelKey);
}
internal void Update(SqlConnection cn)
{
using (SqlCommand cm = cn.CreateCommand())
{
if (this.IsNew)
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "dd_prod_insertcoupon";
AddInsertUpdateParams(cm);
cm.ExecuteNonQuery();
}
else if (this.IsDirty)
{
cm.CommandType = CommandType.Text;
cm.CommandText = "update table_Coupon set MemberIdno=@MemberIdno, Value=@Value, GenDate=@GenDate, " +
"Deadline=@Deadline, isUsed=@isUsed, UseDate=@UseDate, Remark=@Remark, issueHotelKey=@issueHotelKey where pkey=@pkey";
cm.Parameters.AddWithValue("@pkey", _pkey);
AddInsertUpdateParams(cm);
cm.ExecuteNonQuery();
}
MarkOld();
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
SqlConnection cn = new SqlConnection("Data Source=.;User ID=sa;Password=123456;Initial Catalog=tblcustom;");
cn.Open();
try
{
try
{
if (!this.IsNew)
Update(cn);
}
catch (Exception ex)
{
throw (ex);
}
}
finally
{
cn.Close();
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_DeleteSelf()
{
DeleteSelf();
}
public void DeleteSelf()
{
SqlHelper.ExecuteSql("delete from table_Coupon where pkey=" + _pkey.ToString());
}
/// <summary>
/// 获得单条对像的数据
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
internal Tcoupon couponRecord(string where)
{
Tcoupon couponClass = new Tcoupon();
SqlDataReader sqlReader = null;
string sql = "select top 1 * from table_Coupon where 1=1 " + where;
sqlReader = SqlHelper.ExecuteReader(sql);
if (sqlReader.Read())
{
couponClass._pkey = Convert.ToInt32(sqlReader["pkey"]);
couponClass._memberIdno = sqlReader["MemberIdno"].ToString();
couponClass._couponValue = Convert.ToInt32(sqlReader["Value"]);
couponClass._deadline = Convert.ToDateTime(sqlReader["Deadline"]);
couponClass._genDate = Convert.ToDateTime(sqlReader["GenDate"]);
couponClass._isUsed = Convert.ToBoolean(sqlReader["IsUsed"]);
couponClass._memberIdno = sqlReader["MemberIdno"].ToString();
couponClass._remark = sqlReader["Remark"].ToString();
if (sqlReader["UseDate"].ToString() != string.Empty)
{
couponClass._useDate = Convert.ToDateTime(sqlReader["UseDate"]);
}
couponClass._issueHotelKey = Convert.ToInt32(sqlReader["issueHotelKey"]);
}
sqlReader.Close();
MarkOld();
return couponClass;
}
public DataSet getall()
{
return SqlHelper.Query(" select * from table_Coupon ");
}
/// <summary>
/// 获得多条对像的数据
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public void couponRecorddlist(string where)
{
Tcoupon couponClass;
SqlDataReader sqlReader = null;
string sql = "select * from table_Coupon where 1=1 " + where;
sqlReader = SqlHelper.ExecuteReader(sql);
while (sqlReader.Read())
{
couponClass = new Tcoupon();
couponClass._pkey = Convert.ToInt32(sqlReader["pkey"]);
couponClass._memberIdno = sqlReader["MemberIdno"].ToString();
couponClass._couponValue = Convert.ToInt32(sqlReader["Value"]);
couponClass._deadline = Convert.ToDateTime(sqlReader["Deadline"]);
couponClass._genDate = Convert.ToDateTime(sqlReader["GenDate"]);
couponClass._isUsed = Convert.ToBoolean(sqlReader["IsUsed"]);
couponClass._memberIdno = sqlReader["MemberIdno"].ToString();
couponClass._remark = sqlReader["Remark"].ToString();
if (sqlReader["UseDate"].ToString() != string.Empty)
{
couponClass._useDate = Convert.ToDateTime(sqlReader["UseDate"]);
}
couponClass._issueHotelKey = Convert.ToInt32(sqlReader["issueHotelKey"]);
}
sqlReader.Close();
}
}
}
本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢