先看看实体类定义:
代码
using PWMIS.DataMap.Entity;
namespace PLZDModel
{
public class PFT_Report : Entity
{
public PFT_Report()
{
TableName = "PFT_Report";
//IdentityName = "标识列";//如果指定了标识列,可以处理自增列插入问题
//PrimaryKeys.Add("主键列");//指定主键列方可以删除和更新实体数据
AddProperty("ID", default(System.Int32));
AddProperty("FinancialPlanersNo", default(System.String));
AddProperty("FundAccount", default(System.String));
AddProperty("CityCode", default(System.String));
AddProperty("BankCode", default(System.String));
AddProperty("NetWork", default(System.String));
AddProperty("ApplyTime", default(System.DateTime));
AddProperty("FileName", default(System.String));
AddProperty("GenerateTime", default(System.DateTime));
}
public System.Int32 ID
{
get { return (System.Int32)getProperty("ID"); }
set { setProperty("ID", value); }
}
public System.String FinancialPlanersNo
{
get { return (System.String)getProperty("FinancialPlanersNo"); }
set { setProperty("FinancialPlanersNo", value); }
}
public System.String FundAccount
{
get { return (System.String)getProperty("FundAccount"); }
set { setProperty("FundAccount", value); }
}
public System.String CityCode
{
get { return (System.String)getProperty("CityCode"); }
set { setProperty("CityCode", value); }
}
public System.String BankCode
{
get { return (System.String)getProperty("BankCode"); }
set { setProperty("BankCode", value); }
}
public System.String NetWork
{
get { return (System.String)getProperty("NetWork"); }
set { setProperty("NetWork", value); }
}
public System.DateTime ApplyTime
{
get { return (System.DateTime)getProperty("ApplyTime"); }
set { setProperty("ApplyTime", value); }
}
public System.String FileName
{
get { return (System.String)getProperty("FileName"); }
set { setProperty("FileName", value); }
}
public System.DateTime GenerateTime
{
get { return (System.DateTime)getProperty("GenerateTime"); }
set { setProperty("GenerateTime", value); }
}
}
}
namespace PLZDModel
{
public class PFT_Report : Entity
{
public PFT_Report()
{
TableName = "PFT_Report";
//IdentityName = "标识列";//如果指定了标识列,可以处理自增列插入问题
//PrimaryKeys.Add("主键列");//指定主键列方可以删除和更新实体数据
AddProperty("ID", default(System.Int32));
AddProperty("FinancialPlanersNo", default(System.String));
AddProperty("FundAccount", default(System.String));
AddProperty("CityCode", default(System.String));
AddProperty("BankCode", default(System.String));
AddProperty("NetWork", default(System.String));
AddProperty("ApplyTime", default(System.DateTime));
AddProperty("FileName", default(System.String));
AddProperty("GenerateTime", default(System.DateTime));
}
public System.Int32 ID
{
get { return (System.Int32)getProperty("ID"); }
set { setProperty("ID", value); }
}
public System.String FinancialPlanersNo
{
get { return (System.String)getProperty("FinancialPlanersNo"); }
set { setProperty("FinancialPlanersNo", value); }
}
public System.String FundAccount
{
get { return (System.String)getProperty("FundAccount"); }
set { setProperty("FundAccount", value); }
}
public System.String CityCode
{
get { return (System.String)getProperty("CityCode"); }
set { setProperty("CityCode", value); }
}
public System.String BankCode
{
get { return (System.String)getProperty("BankCode"); }
set { setProperty("BankCode", value); }
}
public System.String NetWork
{
get { return (System.String)getProperty("NetWork"); }
set { setProperty("NetWork", value); }
}
public System.DateTime ApplyTime
{
get { return (System.DateTime)getProperty("ApplyTime"); }
set { setProperty("ApplyTime", value); }
}
public System.String FileName
{
get { return (System.String)getProperty("FileName"); }
set { setProperty("FileName", value); }
}
public System.DateTime GenerateTime
{
get { return (System.DateTime)getProperty("GenerateTime"); }
set { setProperty("GenerateTime", value); }
}
}
}
之后,便可以这样使用实体类:
1,查询:
代码
class Program
{
static void Main(string[] args)
{
PFT_Report utlReport = new PFT_Report();
OQL oqlQuerry = new OQL(utlReport);
utlReport.FundAccount = "1234234242423";
oqlQuerry.TopCount = 3;
//OQL 表达式如果单步调试,将会有调试陷阱,Where 子句将可能会带上Select子句中的字段
oqlQuerry.Select(utlReport.ApplyTime , utlReport.BankCode,utlReport.FileName ,utlReport.FundAccount )
.Where(utlReport.FundAccount)
.OrderBy(utlReport.GenerateTime, "desc");
Console.WriteLine ("SQL="+oqlQuerry.ToString ());
PFT_FundReviews p = new PFT_FundReviews();
OQL q = new OQL(p);
q.TopCount = 1;
p.BankCode = null;// "20";
p.FundCode = "KF001";
q.Select().Where(p.BankCode, p.FundCode).OrderBy(p.addDate, "desc");
Console.WriteLine("SQL=" + q.ToString());
Console.Read();
}
}
{
static void Main(string[] args)
{
PFT_Report utlReport = new PFT_Report();
OQL oqlQuerry = new OQL(utlReport);
utlReport.FundAccount = "1234234242423";
oqlQuerry.TopCount = 3;
//OQL 表达式如果单步调试,将会有调试陷阱,Where 子句将可能会带上Select子句中的字段
oqlQuerry.Select(utlReport.ApplyTime , utlReport.BankCode,utlReport.FileName ,utlReport.FundAccount )
.Where(utlReport.FundAccount)
.OrderBy(utlReport.GenerateTime, "desc");
Console.WriteLine ("SQL="+oqlQuerry.ToString ());
PFT_FundReviews p = new PFT_FundReviews();
OQL q = new OQL(p);
q.TopCount = 1;
p.BankCode = null;// "20";
p.FundCode = "KF001";
q.Select().Where(p.BankCode, p.FundCode).OrderBy(p.addDate, "desc");
Console.WriteLine("SQL=" + q.ToString());
Console.Read();
}
}
使用该方式,可以选取实体类指定的字段,而不是一次选取全部字段。这对于一个拥有大字段的表来说很有用。字段的选取都是通过实体类对象.属性 的方式,完全面向对象,用起来有点像LINQ,呵呵。
为啥说读取数据不需要反射?因为PDF.NET的实体类有内置的“属性名-属性值”字典,我们来看看具体是如何实现的:
代码
/// <summary>
/// 根据数据阅读器对象,查询实体对象集合(注意查询完毕将自动释放该阅读器对象)
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static List<T> QueryList(System.Data.IDataReader reader)
{
List<T> list = new List<T>();
using (reader)
{
while (reader.Read())
{
T t = new T();
for (int i = 0; i < reader.FieldCount; i++)
{
if (!reader.IsDBNull(i))
t.setProperty(reader.GetName(i), reader.GetValue(i));
}
list.Add(t);
}
}
return list;
}
/// 根据数据阅读器对象,查询实体对象集合(注意查询完毕将自动释放该阅读器对象)
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static List<T> QueryList(System.Data.IDataReader reader)
{
List<T> list = new List<T>();
using (reader)
{
while (reader.Read())
{
T t = new T();
for (int i = 0; i < reader.FieldCount; i++)
{
if (!reader.IsDBNull(i))
t.setProperty(reader.GetName(i), reader.GetValue(i));
}
list.Add(t);
}
}
return list;
}
直接调用实体类的setProperty 方法即可。
保存数据也很简单,为指定的属性设置新值即可:
PFT_Report utlReport = new PFT_Report();
utlReport.FundAccount = "1234234242423";
EntityQuery<PFT_Report>.Save(utlReport);
utlReport.FundAccount = "1234234242423";
EntityQuery<PFT_Report>.Save(utlReport);
你本次更新了多少个字段,框架只会生成要更新的字段的SQL语句,整个框架可以完全不使用反射来处理数据,保证了数据更新的效率。
整个实体类方案原理很简单,但实体类代码比较多,手写代码还是比较累人,所以框架提供一个代码生成器,从数据库表生成实体类或者将自己编写的SQL语句映射成实体类。