db4o学习笔记(二)

更新对象
在Db4o数据库中更新对象很容易,例如:
None.gif     Student template = new  Student( " Tom " , 0 , null );
None.gif    
// 得到要更新的对象
None.gif
    IObjectSet result = db.Get(template);
None.gif    
foreach ( object  item  in  result)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Student found
=(Student)item;
InBlock.gif        found.Age
+=10;
InBlock.gif        
//更新对象到数据库
InBlock.gif
        db.Set(found);
InBlock.gif        Console.WriteLine(
"Updated {0}",found);
ExpandedBlockEnd.gif    }

None.gif    GetAllStudent(db);
None.gif

删除对象
在Db4o数据库中删除对象使用Delete方法,例如:
None.gif     Student template = new  Student( " Tom " , 0 , null );
None.gif    
// 得到要删除的对象
None.gif
    IObjectSet result = db.Get(template);
None.gif    
foreach ( object  item  in  result)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Student found
=(Student)item;
InBlock.gif        
//删除对象
InBlock.gif
        db.Delete(found);
InBlock.gif        Console.WriteLine(
"Deleted {0}",found);
ExpandedBlockEnd.gif    }

None.gif    GetAllStudent(db);
None.gif

当对象的数据成员也是一个对象时,我们如何操作他们呢。db4o引入了更新深度(update depth)的概念来控制被更新的对象成员树深度。默认的更新深度是 1。
在前面的定义的学生类里有一个myschool数据成员,他是一个学校类,定义如下:
None.gif      // 定义一个学校类
None.gif
     public   class  School
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string name;
InBlock.gif        
private string address;
InBlock.gif
InBlock.gif        
public School()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public School(string name,string address)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.name=name;
InBlock.gif            
this.address=address;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                name
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Address
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return address;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                address
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.Format("name:{0};address:{1}",name,address);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif

使用CascadeOnUpdate方法,可以更新成员对象,该方法必须在每次开启数据库之前设置.例如:
None.gif     Db4oFactory.Configure().ObjectClass( typeof (Student)).CascadeOnUpdate( true );
None.gif    IObjectContainer db
= Db4oFactory.OpenFile( " Student.yap " );
None.gif    
try
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Student template
=new Student("Tom",0,null);
InBlock.gif        
//得到要更新的对象
InBlock.gif
        IObjectSet result=db.Get(template);
InBlock.gif        
foreach(object item in result)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Student found
=(Student)item;
InBlock.gif            found.Age
+=10;
InBlock.gif            
//更新学校对象
InBlock.gif
            found.MySchool.Address="CHINA ANHUI";
InBlock.gif            
//更新对象到数据库
InBlock.gif
            db.Set(found);
InBlock.gif            Console.WriteLine(
"Updated {0}",found);
ExpandedSubBlockEnd.gif        }

InBlock.gif        GetAllStudent(db);
ExpandedBlockEnd.gif    }

None.gif    
finally
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        db.Close();
ExpandedBlockEnd.gif    }

None.gif

和CascadeOnUpdate方法一样,CascadeOnDelete方法用来删除所有对象.例如:
None.gif     Db4oFactory.Configure().ObjectClass( typeof (Student)).CascadeOnDelete( true );
None.gif    IObjectContainer db
= Db4oFactory.OpenFile( " Student.yap " );
None.gif    
try
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Student template
=new Student("Tom",0,null);
InBlock.gif        
//得到要删除的对象
InBlock.gif
        IObjectSet result=db.Get(template);
InBlock.gif        
foreach(object item in result)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Student found
=(Student)item;
InBlock.gif            
//删除对象
InBlock.gif
            db.Delete(found);
InBlock.gif            Console.WriteLine(
"Deleted {0}",found);
ExpandedSubBlockEnd.gif        }

InBlock.gif        GetAllStudent(db);
ExpandedBlockEnd.gif    }

None.gif    
finally
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        db.Close();
ExpandedBlockEnd.gif    }

None.gif

转载于:https://www.cnblogs.com/ustc9702/archive/2007/06/27/797393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值