Previous in Contents Next in Contents

Modifying and Inserting Records with DBStorage

To modify records using DBStorage, read a record into a Dictionary or OrderForm object (using GetData, LookupData, or LookupMultipleData), modify the contents of the object, and then pass the object to CommitData to update the row.

To insert a new record into the database, initialize the appropriate data object, and then pass the object to InsertData to insert the row.

The following example adds an item to a shopping basket. First, a DBStorage object is created to access the database of active shopping baskets. Next, the GetData method is used to retrieve the basket for the current customer. Assuming the basket already exists, it is returned as an OrderForm object called mscsOrderForm. It the basket does not already exist in the database, then an empty OrderForm object is created.

Next, the item is added to the OrderForm using the OrderForm object's AddItem method. Finally, the OrderForm is written to storage. If the OrderForm is new, the data is inserted using the InsertData method; if the order form already existed in the basket database, the revised data is committed using the CommitData method:

REM  Create and initialize the OrderForm storage object
set mscsOrderFormStorage = Server.CreateObject("Commerce.DBStorage")
call mscsOrderFormStorage.InitStorage(MSCSSite.DefaultConnectionString, "Clocktower_basket", "shopper_id", "Commerce.OrderForm", "marshalled_order", "date_changed")

REM  Create an OrderForm for the shopper's basket
created = 0
on error resume next
set mscsOrderForm = mscsOrderFormStorage.GetData(null, mscsShopperID)
on error goto 0
if IsEmpty(mscsOrderForm) then
   set mscsOrderForm = Server.CreateObject("Commerce.OrderForm")
   mscsOrderForm.shopper_id = mscsShopperID
   created = 1
end if

REM  Add the item to the basket
call mscsOrderForm.AddItem(product_sku, product_qty, 0)

REM  Update the database
if created = 0 then
   call mscsOrderFormStorage.CommitData(NULL, mscsOrderForm)
else
   call mscsOrderFormStorage.InsertData(NULL, mscsOrderForm)
end if

Response.Redirect mscsPage.URL("basket.asp")

The CommitData and InsertData methods can store the data from the specified object into the database in two ways.

If the MarshallColumn parameter is specified and matching columns exist, then the corresponding data is stored both ways.


© 1997-2000 Microsoft Corporation. All rights reserved.