先看看實體類定義:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
using PWMIS.SqlMapper.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); } } } } |
之後,便可以這樣使用實體類:
1,查詢:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Program { static void Main( string [] args) { PFT_Report utlReport = new PFT_Report(); OQL oqlQuerry = new OQL(utlReport); utlReport.FundAccount = "1234234242423" ; oqlQuerry.TopCount = 3; oqlQuerry.Select(utlReport.ApplyTime , utlReport.BankCode,utlReport.FileName ,utlReport.FundAccount ) .Where(utlReport.FundAccount) .OrderBy(utlReport.GenerateTime, "desc" ); Console.WriteLine ( "SQL=" +oqlQuerry.ToString ()); Console.Read(); } |
運行程序將輸出:
SQL=SELECT Top 3 ApplyTime,BankCode,FileName,FundAccount
FROM PFT_Report
Where FundAccount=@FundAccount
Order by FundAccount desc
------------
使用該方式,可以選取實體類指定的字段,而不是一次選取全部字段。這對於一個擁有大字段的表來說很有用。字段的選取都是通過實體類對象.屬性 的方式,完全面向對象,用起來有點像LINQ,呵呵。
保存數據也很簡單,爲指定的屬性設置新值即可:
1
2
3
|
PFT_Report utlReport = new PFT_Report(); utlReport.FundAccount = "1234234242423" ; EntityQuery<PFT_Report>.Save(utlReport); |
你本次更新了多少個字段,框架只會生成要更新的字段的SQL語句,保證了數據更新的效率。