Product Documentation

c-treeDB API for VB.Net

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

'

' Manage()

'

' This function performs simple record functions of add, delete and gets

'

Sub Manage()

Console.WriteLine("MANAGE")

' populate the tables with data

Add_CustomerMaster_Records()

Add_ItemMaster_Records()

Add_Transactions()

' display the orders and their items

Display_CustomerOrders()

Display_OrderItems()

End Sub

'

' Add_CustomerMaster_Records()

'

' This function adds records to table CustomerMaster from an

' array of strings

'

Public Structure CUSTOMER_DATA

' struct members

Public number, zipcode, state, rating, name, address, city As String

' struct constructor

Public Sub New(ByVal number As String, ByVal zipcode As String, ByVal state As String, ByVal rating As String, ByVal name As String, ByVal address As String, ByVal city As String)

Me.number = number

Me.zipcode = zipcode

Me.state = state

Me.rating = rating

Me.name = name

Me.address = address

Me.city = city

End Sub

End Structure

Sub Add_CustomerMaster_Records()

Dim data(3) As CUSTOMER_DATA

data(0) = New CUSTOMER_DATA("1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange")

data(1) = New CUSTOMER_DATA("1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford")

data(2) = New CUSTOMER_DATA("1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta")

data(3) = New CUSTOMER_DATA("1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia")

Dim nRecords As Integer = data.Length

Delete_Records(recordCustMast)

Console.WriteLine(ControlChars.Tab + "Add records in table CustomerMaster...")

Try

' start a transaction

recordCustMast.Begin()

Dim i As Integer

For i = 0 To nRecords - 1

recordCustMast.Clear()

' populate record buffer with data

recordCustMast.SetFieldAsString(0, data(i).number)

recordCustMast.SetFieldAsString(1, data(i).zipcode)

recordCustMast.SetFieldAsString(2, data(i).state)

recordCustMast.SetFieldAsString(3, data(i).rating)

recordCustMast.SetFieldAsString(4, data(i).name)

recordCustMast.SetFieldAsString(5, data(i).address)

recordCustMast.SetFieldAsString(6, data(i).city)

' add record

recordCustMast.Write()

Next

' commit transaction

recordCustMast.Commit()

Catch E As CTException

Handle_Exception(E)

End Try

End Sub

'

' Add_ItemMaster_Records()

'

' This function adds records to table ItemMaster from an

' array of strings

'

Public Structure ITEM_DATA

' struct members

Public weight As Integer

Public price As CTMoney

Public itemnum, description As String

' struct constructor

Public Sub New(ByVal weight As Integer, ByVal price As CTMoney, ByVal itemnum As String, ByVal description As String)

Me.weight = weight

Me.price = price

Me.itemnum = itemnum

Me.description = description

End Sub

End Structure

Sub Add_ItemMaster_Records()

Dim data(3) As ITEM_DATA

Data(0) = New ITEM_DATA(10, 1995, "1", "Hammer")

Data(1) = New ITEM_DATA(3, 999, "2", "Wrench")

Data(2) = New ITEM_DATA(4, 1659, "3", "Saw")

Data(3) = New ITEM_DATA(1, 398, "4", "Pliers")

Dim nRecords As Integer = Data.Length

Delete_Records(recordItemMast)

Console.WriteLine(ControlChars.Tab + "Add records in table ItemMaster...")

Try

' start a transaction

recordItemMast.Begin()

Dim i As Integer

For i = 0 To nRecords - 1

recordItemMast.Clear()

' populate record buffer with data

recordItemMast.SetFieldAsSigned(0, Data(i).weight)

recordItemMast.SetFieldAsMoney(1, Data(i).price)

recordItemMast.SetFieldAsString(2, Data(i).itemnum)

recordItemMast.SetFieldAsString(3, Data(i).description)

' add record

recordItemMast.Write()

Next

' commit transaction

recordItemMast.Commit()

Catch E As CTException

Handle_Exception(E)

End Try

End Sub

'

' Delete_Records()

'

' This function deletes all the records in the table

'

Sub Delete_Records(ByVal record As CTRecord)

Dim found As Boolean

Console.WriteLine(ControlChars.Tab + "Delete records...")

Try

' write lock required for transaction updates

record.Lock(LOCK_MODE.WRITE_LOCK)

' start a transaction

record.Begin()

' read first record

found = record.First()

While (found) ' while records are found

' delete record

record.Delete()

' read next record

found = record.Next()

End While

' commit transaction

record.Commit()

' free locks

record.Unlock()

Catch E As CTException

record.Abort()

Handle_Exception(E)

End Try

End Sub

'

' Add_Transactions()

'

' Add an Order and associated Items "as a transaction" to their

' respective tables. A transaction is committed or aborted if the

' customer number on the order is confirmed valid. Likewise each

' item in the order is verified to be a valid item. SavePoints are

' established as an order is processed, allowing a transaction to

' rollback to the previously verified item

'

Public Structure ORDER_DATA

' struct members

Public orderdate, promdate, ordernum, custnum As String

' struct constructor

Public Sub New(ByVal orderdate As String, ByVal promdate As String, ByVal ordernum As String, ByVal custnum As String)

Me.orderdate = orderdate

Me.promdate = promdate

Me.ordernum = ordernum

Me.custnum = custnum

End Sub

End Structure

Public Structure ORDERITEM_DATA

' struct members

Public ordernum As String

Public seqnumber, quantity As Integer

Public itemnum As String

' struct constructor

Public Sub New(ByVal ordernum As String, ByVal seqnumber As Integer, ByVal quantity As Integer, ByVal itemnum As String)

Me.ordernum = ordernum

Me.seqnumber = seqnumber

Me.quantity = quantity

Me.itemnum = itemnum

End Sub

End Structure

Sub Add_Transactions()

Dim orders(2) As ORDER_DATA

orders(0) = New ORDER_DATA("09/01/2002", "09/05/2002", "1", "1001")

orders(1) = New ORDER_DATA("09/02/2002", "09/06/2002", "2", "9999") ' bad customer number

orders(2) = New ORDER_DATA("09/22/2002", "09/26/2002", "3", "1003")

Dim nOrders As Integer = orders.Length

Dim items(5) As ORDERITEM_DATA

items(0) = New ORDERITEM_DATA("1", 1, 2, "1")

items(1) = New ORDERITEM_DATA("1", 2, 1, "2")

items(2) = New ORDERITEM_DATA("2", 1, 1, "3")

items(3) = New ORDERITEM_DATA("2", 2, 3, "4")

items(4) = New ORDERITEM_DATA("3", 1, 2, "3")

items(5) = New ORDERITEM_DATA("3", 2, 2, "99") ' bad item number

Dim nItems As Integer = items.Length

Dim orderdate As CTDate = New CTDate()

Dim promdate As CTDate = New CTDate()

Dim savepoint As Integer

Dim j As Integer = 0

Delete_Records(recordCustOrdr)

Delete_Records(recordOrdrItem)

Console.WriteLine(ControlChars.Tab + "Add Transaction Records...")

' process orders

Dim i As Integer

For i = 0 To nOrders - 1

' start a transaction

MySession.Begin()

Try

recordCustOrdr.Clear()

' populate record buffer with order data

orderdate.StringToDate(orders(i).orderdate, DATE_TYPE.MDCY_DATE)

promdate.StringToDate(orders(i).promdate, DATE_TYPE.MDCY_DATE)

recordCustOrdr.SetFieldAsDate(0, orderdate)

recordCustOrdr.SetFieldAsDate(1, promdate)

recordCustOrdr.SetFieldAsString(2, orders(i).ordernum)

recordCustOrdr.SetFieldAsString(3, orders(i).custnum)

' add order record

recordCustOrdr.Write()

Catch E As CTException

Handle_Exception(E)

End Try

' set transaction savepoint

savepoint = recordCustOrdr.SetSavePoint()

' process order items

While (items(j).ordernum = orders(i).ordernum)

Try

recordOrdrItem.Clear()

' populate record buffer with order item data

recordOrdrItem.SetFieldAsSigned(0, items(j).seqnumber)

recordOrdrItem.SetFieldAsSigned(1, items(j).quantity)

recordOrdrItem.SetFieldAsString(2, items(j).ordernum)

recordOrdrItem.SetFieldAsString(3, items(j).itemnum)

' add order item record

recordOrdrItem.Write()

' check that item exists in ItemMaster table

recordItemMast.Clear()

recordItemMast.SetFieldAsString(2, items(j).itemnum)

If (recordItemMast.Find(FIND_MODE.EQ) <> True) Then

' if not found, restore back to previous savepoint

recordItemMast.RestoreSavePoint(savepoint)

Else

' set transaction savepoint

savepoint = recordItemMast.SetSavePoint()

End If

Catch E As CTException

Handle_Exception(E)

End Try

' bump to next item

j += 1

' exit the while loop on last item

If (j >= nItems) Then

Exit While

End If

End While

' check that customer exists in CustomerMaster table

recordCustMast.Clear()

recordCustMast.SetFieldAsString(0, orders(i).custnum)

' commit or abort the transaction

If (recordCustMast.Find(FIND_MODE.EQ) <> True) Then

MySession.Abort()

Else

MySession.Commit()

End If

Next

End Sub

'

' Display_CustomerOrders()

'

' This function displays the contents of a table. ctdbFirstRecord() and

' ctdbNextRecord() fetch the record. Then each field is parsed and displayed

'

Sub Display_CustomerOrders()

Dim custnumb As String

Dim ordrnumb As String

Console.WriteLine(ControlChars.Tab + "CustomerOrder table...")

Try

' read first record

If (recordCustOrdr.First()) Then

Do

ordrnumb = recordCustOrdr.GetFieldAsString(2)

custnumb = recordCustOrdr.GetFieldAsString(3)

' display data

Console.WriteLine(ControlChars.Tab + " {0} {1}", ordrnumb, custnumb)

Loop While (recordCustOrdr.Next()) ' read next record until end of file

End If

Catch E As CTException

Handle_Exception(E)

End Try

End Sub

'

' Display_OrderItems()

'

' This function displays the contents of a table. ctdbFirstRecord() and

' ctdbNextRecord() fetch the record. Then each field is parsed and displayed

'

Sub Display_OrderItems()

Dim itemnumb As String

Dim ordrnumb As String

Console.WriteLine(ControlChars.NewLine + ControlChars.Tab + "OrderItems Table...")

Try

' read first record

If (recordOrdrItem.First()) Then

Do

' get field data from record buffer

ordrnumb = recordOrdrItem.GetFieldAsString(2)

itemnumb = recordOrdrItem.GetFieldAsString(3)

' display data

Console.WriteLine(ControlChars.Tab + " {0} {1}", ordrnumb, itemnumb)

Loop While (recordOrdrItem.Next()) ' read next record until end of file

End If

Catch E As CTException

Handle_Exception(E)

End Try

End Sub

TOCIndex