C# EasyORM

申明:本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,我的博客欢迎复制共享,但在同时,希望保留我的署名权CHARSET,并且,不得用于商业用途。如您有任何疑问或者授权方面的协商,请联系我。

使用反射和配置来实现SQL->.NET类的映射

1 SELECT Field1,Field2,Field3 FROM table_name WHERE Field4=?

上述例子是由于OleDb不支持命名参数,以下均为OleDb作为例子。

配置如下:

 1 <Statement>
 2   <SQL><!CDATA[[SELECT Field1,Field2,Field3 FROM table_name WHERE Field4=?]]></SQL>
 3   <Parameters>
 4     <Parameter Name="@Field4" Type="System.String" />
 5   </Parameters>
 6   <Result ReturnList="False/True" Type="Query.Information.ClientInfo,Query.Information.ClientInfo">
 7     <Field Name="Field1" Mapping="Dummy" Type="System.Decimal" />
 8     <Field Name="Field2" Mapping="Name" Type="System.String" />
 9     <Field Name="Field3" Mapping="Salary" Type="System.Double" />
10   </Result>
11 </Statement>

类定义是:

1 public classClientInfo{
2   [Entity]public decimal Dummy{ get; set; }
3   [Entity]public string Name{get;set; }
4   [Entity]public double Salary{get;set; }
5 }

使用下面函数来获得类的示例:

1 object SQLHelper.Execute(XXXConnection connection, string profile, Dictionary<string, string> KV);

方法如下:

1 Dictionary<string,string> KV = new Dictionary<string,string>();
2 KV.Add("@Field4", "SID0001");
3 Query.Information.ClientInfo clientInfo
4   = SQLHelper.Execute(connection, "获得产品信息", KV) as Query.Information.ClientInfo;

或者:

1 Dictionary<string,string> KV = newDictionary<string,string>();
2 KV.Add("@Field4", "SID0001");
3 List<Query.Information.ClientInfo>clientInfo
4   = SQLHelper.Execute(connection, "获得产品信息", KV) as List<Query.Information.ClientInfo>;
时间: 2024-08-22 15:32:39

C# EasyORM的相关文章