浏览 2690 次
锁定老帖子 主题:介绍一个.NET的活动记录框架
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-02-14
最近写了一个活动记录框架,现介绍一下基本特点:
先看一下类的定义 [Table("products")] public class Product : ActiveRecordBase
{
public Product()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[PrimaryKey]
public int id
{
get {return _id;}
set {_id = value;}
}
[Field]
public string name
{
get {return _name;}
set {_name = value;}
}
[Field]
public string shape
{
get {return _shape;}
set {_shape = value;}
}
[Field]
public int amount
{
get {return _amount;}
set {_amount = value;}
}
[Field]
public string remark
{
get {return _remark;}
set {_remark = value;}
}
private int _id;
private string _name;
private string _shape;
private int _amount;
private string _remark;
}
现在我们可以进行一些基本的CRUD操作了
1、增加记录: Product p = new Product();
p.name = "电脑";
p.shape = "PII";
p.amount = 30;
p.Create();
2、按关键字查询数据: Product p = (Product)Product.Find(typeof(Product),1);
Console.WriteLine(p.id);
Console.WriteLine(p.name);
Console.WriteLine(p.shape);
Console.WriteLine(p.amount);
3、修改数据:
Product p = (Product)Product.Find(typeof(Product),1);
p.shape = "PIII";
p.amount = 23;
p.remark = "备用";
p.Update();
4、删除记录:
Product p = (Product)Product.Find(typeof(Product),1);
p.Destroy();
5、查询所有的数据:
IList ll = Product.FindAll(typeof(Product));
foreach(Product p in ll)
{
Console.WriteLine(p.name+","+p.shape+","+p.remark);
} 6、查询数量大于5,并且备注包含“中国”开头,按照名称反序排列,最多只取2条记录:
IList ll = Product.FindAll(typeof(Product),"amount>? and remark like ?",new object[]{5,"中国%"},"name desc",2);
foreach(Product p in ll)
{
Console.WriteLine(p.name+","+p.shape+","+p.remark);
}
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |