您的位置:首页 > 数据库

Websharp使用说明(4)

2004-02-26 10:51 477 查看
       数据的存取方式<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

数据存取的目的,是持久化保存对象。在Websharp中,定义了PersistenceManager接口来实现这个功能。PersistenceManager的定义可以见:附1:Websharp主要接口定义——PersistenceManager
 

我们可以使用如下的方式来持久化保存一个对象:
Product product=new Product (true);

……//处理product

PersistenceManager pm = PersistenceManagerFactory.Instance().

CreatePersistenceManager();

     pm.PersistNewObject(p);

     pm.Close();
 

代码非常简明和直观,没有一大堆数据库操纵的代码,也不容易发生差错。
也可以通过向PersistenceManagerFactory 传递一个PersistenceProperty参数来初始化一个PersistenceManager,如:
PersistenceProperty pp=new PersistenceProperty();

pp……//设置pp的属性

PersistenceManager pm = PersistenceManagerFactory.Instance().CreatePersistenceManager(pp);
关于PersistenceProperty的说明,可以见后面的系统持久化配置信息一节。
 

       事务处理

在很多时候,在处理对象保存的时候,我们需要使用事务处理,特别是在处理上上面示例中的类似于入库单的一对多结构的对象的时候。在Websharp中,我们可以通过Transaction 接口来完成这个功能。Transaction接口的定义可以见:附1:Websharp主要接口定义——Transaction
       下面是使用事务处理的一个例子:
Product product=new Product (true);

……//处理product

PersistenceManager pm = PersistenceManagerFactory.Instance().

CreatePersistenceManager();

     Transaction trans=pm.CurrentTransaction;

trans.Begin();

try

{

          pm.PersistNewObject(p);

          trans.Commit();

}

catch(Excption e)

{

          trans.Rollback();

}

finally

{

          pm.Close();

}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库