写过一段时间的DOTNET程序和ROR程序后,受ROR的ActiveRecord框架启发,故此写了DOTNET版本的活动记录框架。具有CRUD、事务、验证器、支持多数据库连接。
先介绍简单的CRUD操作。
建立表结构:
create table products(
id int primary key identity,
name varchar(50),
shape varchar(50),
amount int,
remark varchar(200)
)
然后定义类:
using EtNet.ActiveRecord;
[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;
}
1、增加记录
Product p = new Product();
p.name = "电脑";
p.shape = "PII";
p.amount = 30;
p.Create();
2、修改记录
Product p = (Product)Product.Find(typeof(Product),1);
p.shape = "PIII";
p.amount = 23;
p.remark = "备用";
p.Update();
3、删除记录
Product p = (Product)Product.Find(typeof(Product),1);
p.Destroy();
4、查询记录
Product p = (Product)Product.Find(typeof(Product),1);
Console.WriteLine(p.id);
Console.WriteLine(p.name);
Console.WriteLine(p.shape);
Console.WriteLine(p.amount);
其他的功能操作以后再介绍。