论坛首页 编程语言技术论坛

活动记录框架

浏览 1602 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-06-22  
写过一段时间的DOTNET程序和ROR程序后,受ROR的ActiveRecord框架启发,故此写了DOTNET版本的活动记录框架。具有CRUD、事务、验证器、支持多数据库连接。

先介绍简单的CRUD操作。

建立表结构:

Csharp代码 
create table products(  
id int primary key identity,  
name varchar(50),  
shape varchar(50),  
amount int,  
remark varchar(200)  


create table products(
id int primary key identity,
name varchar(50),
shape varchar(50),
amount int,
remark varchar(200)
)


然后定义类:

Csharp代码 
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;  


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、增加记录

Csharp代码 
Product p = new Product();  
p.name = "电脑";  
p.shape = "PII";  
p.amount = 30;  
p.Create(); 

Product p = new Product();
p.name = "电脑";
p.shape = "PII";
p.amount = 30;
p.Create();


2、修改记录

Csharp代码 
Product p = (Product)Product.Find(typeof(Product),1);  
p.shape = "PIII";  
p.amount = 23;  
p.remark = "备用";  
p.Update(); 

Product p = (Product)Product.Find(typeof(Product),1);
p.shape = "PIII";
p.amount = 23;
p.remark = "备用";
p.Update();


3、删除记录

Csharp代码 
Product p = (Product)Product.Find(typeof(Product),1);  
p.Destroy(); 

Product p = (Product)Product.Find(typeof(Product),1);
p.Destroy();


4、查询记录

Csharp代码 
Product p = (Product)Product.Find(typeof(Product),1);  
Console.WriteLine(p.id);  
Console.WriteLine(p.name);  
Console.WriteLine(p.shape);  
Console.WriteLine(p.amount); 

Product p = (Product)Product.Find(typeof(Product),1);
Console.WriteLine(p.id);
Console.WriteLine(p.name);
Console.WriteLine(p.shape);
Console.WriteLine(p.amount);

其他的功能操作以后再介绍。
   发表时间:2011-09-19  
备用,希望你尽快更新
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics