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 coupon:BusinessBase<coupon>
{
#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 coupon()
{
//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
//public override string ToString()
//{
// return "Table_Coupon" + "/" + _pkey.ToString();
//}
//public bool Equals(coupon Coupon)
//{
// return _pkey.Equals(Coupon.pkey);
//}
//public override int GetHashCode()
//{
// return ("Table_Coupon" + "/" + _pkey.ToString()).GetHashCode();
//}
protected override object GetIdValue()
{
return _pkey;
}
#endregion //System.Object Overrides
#region Static Methods
public static coupon NewCoupon()
{
return new coupon();
}
public static coupon GetCoupon(coupon Tcoupon)
{
return new coupon(Tcoupon);
}
public static coupon GetCoupon1(int Tcouponid)
{
return new coupon(Tcouponid);
}
internal coupon(coupon Tcoupon)
{
FetchObject(Tcoupon);
//MarkAsChild();
}
internal coupon(int key)
{
FetchObject(couponRecord(" and pkey=" + key.ToString()));
}
#endregion
private void FetchObject(coupon Tcoupon)
{
_pkey = Tcoupon._pkey;
_couponValue = Tcoupon._couponValue;
_deadline = Tcoupon._deadline;
_genDate = Tcoupon._genDate;
_isUsed = Tcoupon._isUsed;
_memberIdno = Tcoupon._memberIdno;
_remark = Tcoupon._remark;
_useDate = Tcoupon._useDate;
_issueHotelKey = Tcoupon._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 coupon couponRecord(string where)
{
coupon couponClass = new coupon();
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;
}
}
}
表示层代码:
coupon Tcoupon = coupon.GetCoupon1(21);
Label1.Text = Tcoupon.MemberIdNo + "_" + Tcoupon.pkey.ToString()+"_" +Tcoupon.e;
//coupon Tcoupon2 = coupon.NewCoupon();
Tcoupon.CouponValue = 3121;
Tcoupon.Save();
//Tcoupon2.CouponValue = 610;
//Tcoupon2.MemberIdNo = "Dlll1";
//Tcoupon2.Save();
Tcoupon.Delete();
本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢