• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

DAO Examples

Started by José Roca, August 21, 2011, 02:18:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca


' ########################################################################################
' Parses the Properties collection
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine         ' Pointer to the DbEngine object
   LOCAL pWrkMain AS DAOWorkspace         ' Pointer to the main Workspace object
   LOCAL pDatabase AS DAODatabase         ' Pointer to the Database object
   LOCAL pProperties AS DAOProperties     ' Pointer to the Properties collection
   LOCAL pProperty AS DAOProperty         ' Pointer to the Property object
   LOCAL nCount AS LONG                   ' Number of objects in the collection
   LOCAL i AS LONG                        ' Loop counter variable

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION

   TRY
      ' Create Microsoft Jet Workspace object
      pWrkMain = pDBEngine.CreateWorkSpace("", "admin", "", %dbUseJet)
      ' Open the Database
      pDatabase = pWrkMain.OpenDatabase(EXE.Path$ & "BIBLIO.MDB", %FALSE)
      ' Enumerate the Properties collection of the Database object.
      PRINT "Database properties for " & pDatabase.Name
      pProperties = pDatabase.Properties
      IF ISNOTHING(pProperties) THEN EXIT TRY
      ' Get the number of properties
      nCount = pProperties.Count
      ' Parse the Properties collection
      FOR i = 0 TO nCount - 1
         ' Get a reference to the property object
         pProperty = pProperties.Item(i)
         IF ISOBJECT(pProperty) THEN
            ' Show the name of the property
            PRINT pProperty.Name
            pProperty = NOTHING
         END IF
      NEXT
      pProperties = NOTHING
   CATCH
      STDOUT DaoGetErrorInfo(pDbEngine, OBJRESULT)
   FINALLY
      ' Close the database
      IF ISOBJECT(pDatabase) THEN pDatabase.Close
      ' Close the Workspace
      IF ISOBJECT(pWrkMain) THEN pWrkMain.Close
   END TRY

   WAITKEY$

END FUNCTION
' ========================================================================================


José Roca


' ########################################################################################
' Parses the Properties collection
' This example uses the OpenDatabase method of the DbEngine interface instead of
' a workspace.
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine         ' Pointer to the DbEngine object
   LOCAL pDatabase AS DAODatabase         ' Pointer to the Database object
   LOCAL pProperties AS DAOProperties     ' Pointer to the Properties collection
   LOCAL pProperty AS DAOProperty         ' Pointer to the Property object
   LOCAL nCount AS LONG                   ' Number of objects in the collection
   LOCAL i AS LONG                        ' Loop counter variable

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION

   TRY
      ' Open the Database
      pDatabase = pDbEngine.OpenDatabase(EXE.Path$ & "BIBLIO.MDB")
      ' Enumerate the Properties collection of the Database object.
      PRINT "Database properties for " & pDatabase.Name
      pProperties = pDatabase.Properties
      IF ISNOTHING(pProperties) THEN EXIT TRY
      ' Get the number of properties
      nCount = pProperties.Count
      ' Parse the Properties collection
      FOR i = 0 TO nCount - 1
         ' Get a reference to the property object
         pProperty = pProperties.Item(i)
         IF ISOBJECT(pProperty) THEN
            ' Show the name of the property
            PRINT pProperty.Name
            pProperty = NOTHING
         END IF
      NEXT
      pProperties = NOTHING
   CATCH
      STDOUT DaoGetErrorInfo(pDbEngine, OBJRESULT)
   FINALLY
      ' Close the database
      IF ISOBJECT(pDatabase) THEN pDatabase.Close
   END TRY

   WAITKEY$

END FUNCTION
' ========================================================================================


José Roca


' ########################################################################################
' This example creates a new QueryDef object and appends it to the QueryDefs collection in
' the current database. Then the example enumerates all the properties of the new QueryDef
' and finally deletes it.
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"
#INCLUDE ONCE "AfxVarToStr.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine         ' Pointer to the DbEnfine object
   LOCAL pDatabase AS DAODatabase         ' Pointer to the Database object
   LOCAL pNewQueryDef AS DAOQueryDef      ' Pointer to the QueryDef object
   LOCAL pQueryDef AS DAOQueryDef         ' Pointer to the QueryDef object
   LOCAL pQueryDefs AS DAOQueryDefs       ' Pointer to the QueryDefs collection

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION

   TRY
      ' Open the Database
      pDatabase = pDbEngine.OpenDatabase(EXE.Path$ & "BIBLIO.MDB")
      ' Create a query definition
      pNewQueryDef = pDatabase.CreateQueryDef("This is a test")
      IF ISOBJECT(pNewQueryDef) THEN
         ' Enumerate built-in properties of the new QueryDef
         PRINT "qdfTest.Name: " & pNewQueryDef.Name
         PRINT "qdfTest.DateCreated: " & AfxVarToStr(pNewQueryDef.DateCreated)
         PRINT "qdfTest.LastUpdated: " & AfxVarToStr(pNewQueryDef.LastUpdated)
         PRINT "qdfTest.SQL: " & pNewQueryDef.SQL
         PRINT "qdfTest.ODBCTimeout: " & STR$(pNewQueryDef.ODBCTimeout)
         PRINT "qdfTest.Updatable: " & STR$(pNewQueryDef.Updatable)
         PRINT "qdfTest.Type: " & STR$(pNewQueryDef.Type)
         PRINT "qdfTest.Connect: " & pNewQueryDef.Connect
         PRINT "qdfTest.ReturnsRecords: " & STR$(pNewQueryDef.ReturnsRecords)
         ' Delete the QueryDef
'         pQueryDefs = pDatabase.QueryDefs
'         IF ISOBJECT(pQueryDefs) THEN
'            pQueryDefs.Delete "This is a test"
'            pQueryDefs = NOTHING
'         END IF
         ' The following compound instruction replaces the above remed lines
         pDatabase.QueryDefs.Delete("This is a test")
         pNewQueryDef = NOTHING
      END IF
   CATCH
      STDOUT DaoGetErrorInfo(pDbEngine, OBJRESULT)
   FINALLY
      ' Close the database
      IF ISOBJECT(pDatabase) THEN pDatabase.Close
   END TRY

   WAITKEY$

END FUNCTION
' ========================================================================================


José Roca


' ########################################################################################
' This example creates a new snapshot-type Recordset object and opens it, appending it to
' the Recordsets collection in the default database. It then finds a record and displays it.
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine          ' Pointer to the DbEnfine object
   LOCAL pDatabase AS DAODatabase          ' Pointer to the Database object
   LOCAL pRecordset AS DAORecordset        ' Pointer to the Recordset object
   LOCAL pFields AS DAOFields              ' Pointer to the Fields collection
   LOCAL pField AS DAOField                ' Pointer to the Field object
   LOCAL vValue AS VARIANT                 ' Value

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION

   TRY
      ' Open the Database
      pDatabase = pDbEngine.OpenDatabase(EXE.Path$ & "BIBLIO.MDB")
      ' Open recordset on Authors table
      pRecordset = pDatabase.OpenRecordset("Authors", %dbOpenSnapshot)
      IF pRecordset.RecordCount > 0 THEN
         pRecordset.FindFirst("Author Like '*CA*'")
         DO
            IF ISTRUE pRecordset.NoMatch THEN EXIT DO
            vValue = pRecordset.Collect("Author")
            PRINT VARIANT$$(vValue)
            pRecordset.FindNext("Author Like '*CA*'")
         LOOP
      END IF
   CATCH
      STDOUT DaoGetErrorInfo(pDbEngine, OBJRESULT)
   FINALLY
      ' Close the recordset
      IF ISOBJECT(pRecordset) THEN pRecordset.Close
      ' Close the database
      IF ISOBJECT(pDatabase) THEN pDatabase.Close
   END TRY

   WAITKEY$

END FUNCTION
' ========================================================================================


José Roca


' ########################################################################################
' This example retrieves the version number of the database engine.
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine          ' Pointer to the DbEnfine object

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION
   ' Show the version number
   PRINT "Version: " & pDbEngine.Version
   WAITKEY$

END FUNCTION
' ========================================================================================


José Roca


' ########################################################################################
' This example enumerates the workspaces of the database engine.
' ########################################################################################

' CSED_PBCC ' Use PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "DAO.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pDbEngine AS DAODBEngine         ' Pointer to the DbEngine object
   LOCAL pWorkSpaces AS DAOWorkSpaces     ' Pointer to the Workspaces collection
   LOCAL pWorkSpace AS DAOWorkSpace       ' Pointer to the Workspace object
   LOCAL nCount AS LONG                   ' Number of workspaces
   LOCAL i AS LONG                        ' Loop counter

   ' Create a new DBEngine object
   pDbEngine = NEWCOM "DAO.DBEngine.36"
   IF ISNOTHING(pDbEngine) THEN EXIT FUNCTION

   TRY
      ' Get a reference to the Workspaces collection
      pWorkSpaces = pDbEngine.Workspaces
      IF ISNOTHING(pWorkSpaces) THEN EXIT TRY
      ' Retrieve the number of workspaces
      nCount = pWorkspaces.Count
      ' Enumerate the workspaces
      FOR i = 0 TO nCount - 1
         pWorkSpace = pWorkspaces.Item(i)
         IF ISOBJECT(pWorkSpace) THEN
            PRINT "Name: " & pWorkspace.Name
            PRINT "UserName: " & pWorkspace.UserName
            ' Release the Workspace object
            pWorkSpace = NOTHING
         END IF
      NEXT
      ' Release the workspaces collection
      pWorkSpaces = NOTHING
      ' Display built-in properties.
      PRINT "DBEngine Version: " & pDbEngine.Version
      PRINT "DBEngine LoginTimeout: " & STR$(pDbEngine.LoginTimeout)
   CATCH
      STDOUT DaoGetErrorInfo(pDbEngine, OBJRESULT)
   END TRY

   WAITKEY$

END FUNCTION
' ========================================================================================