db4o学习笔记(二)

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

    GetAllStudent(db);

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

    GetAllStudent(db);

当对象的数据成员也是一个对象时,我们如何操作他们呢。db4o引入了更新深度(update depth)的概念来控制被更新的对象成员树深度。默认的更新深度是 1。
在前面的定义的学生类里有一个myschool数据成员,他是一个学校类,定义如下:
    //定义一个学校类
    public class School
    
{
        
private string name;
        
private string address;

        
public School()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public School(string name,string address)
        
{
            
this.name=name;
            
this.address=address;
        }


        
public string Name
        
{
            
get
            
{
                
return name;
            }

            
set
            
{
                name
=value;
            }

        }


        
public string Address
        
{
            
get
            
{
                
return address;
            }

            
set
            
{
                address
=value;
            }

        }


        
public override string ToString()
        
{
            
return string.Format("name:{0};address:{1}",name,address);
        }


    }


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

        GetAllStudent(db);
    }

    
finally
    
{
        db.Close();
    }


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

        GetAllStudent(db);
    }

    
finally
    
{
        db.Close();
    }

posted on 2007-06-27 11:01  panda  阅读(163)  评论(0编辑  收藏  举报