Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Introducing the CursorAdapter Class : Page 6

One of the most exciting new features of Visual FoxPro 8 is the CursorAdapter class, which provides a common interface for working with data from many different sources. Chuck takes you with him on an adventure in exploring how to use CursorAdapter to change the way you relate to data in VFP 8, whether native tables, ODBC, OLE DB, or XML.


advertisement

CursorAdapter with OLE DB
Our next task is to see how to use OLE DB with the CursorAdapter class, and to compare it to how we've used Native and ODBC data sources. OLE DB technology is more capable than ODBC, and may provide access to more types of data sources than ODBC. The CursorAdapter uses OLE DB by hooking into the objects of ADO, which is the standard COM wrapper around the OLE DB technology. VFP will automatically convert the ADO RecordSet into a VFP cursor for us, and will also handle the updating, just as in the previous examples.

The first thing to do, of course, is to create a new CursorAdapter class. This time, let's build one through code.

Start by creating a new program called caADO.prg, and add the following code:

   PUBLIC goCAADO as CursorAdapter 
   goCAADO = CREATEOBJECT('caADO')
   BROWSE 
   DEFINE CLASS caADO AS CursorAdapter
     oConn = NULL
     oRS = NULL
     Alias = "cCustADO"
     DataSourceType = "ADO"
     SelectCmd = "SELECT " + ;
       "CustomerID, CompanyName, ContactName, "+;
       "ContactTitle, Address, City, Country "+;
       "FROM Customers WHERE Customerid LIKE 'C%'"
     FUNCTION Init()    
       This.DataSource = this.oRS
       This.oRS.ActiveConnection = this.oConn
       This.CursorFill()
     ENDFUNC
   ENDDEFINE
In this code, we set the DataSourceType to ADO and add our usual Customers query to the SelectCmd. When the DataSourceType is ADO, then the DataSource property must contain either a valid RecordSet or Command object, depending upon how you want to use the CursorAdapter. If you don't parameterize your query (like the earlier examples through use of the "?" character) then you can use a RecordSet; otherwise, you are forced to use the Command object, simply because that's where ADO has placed the parameters collection. Any parameters in your query are automatically handled by objects in the parameters collection of the command object.

In this case, we'll use the RecordSet object, but notice that we must also provide a Connection object. In both cases, I am taking advantage of Access methods to create the references to these objects. Listing 3 shows the code for the Access methods.

Both Access methods first check to see if the object has already been created. If not, then they proceed with the object creation. In the case of the RecordSet, you need only to create the object, as the CursorAdapter takes care of the rest. With the Connection object, you must provide the connection string and open the connection, because the CursorAdapter does not open the connection for you. This is because the connection isn't a property of the CursorAdapter, but instead is a property of the RecordSet object.

With this code in place, you can run the program and see the resultant cursor. It should look very much like the cursor you retrieved using ODBC, since it contains the data from the same source.

Updating with OLE DB
Without setting a few more properties, this simple CursorAdapter is not updatable. The following additional code, inserted in the class definition before the Init method, will allow automatic updates to occur:

   KeyFieldList = "CustomerID"
   UpdatableFieldList = ;
     "CompanyName, ContactName, ContactTitle, "+ ;
     "Address, City, Country"
   UpdateNameList = ;
     "CustomerID Customers.CustomerID, " + ;
     "CompanyName Customers.CompanyName, " + ;
     "ContactName Customers.ContactName, "+;
     "ContactTitle Customers.ContactTitle, " + ;
     "Address Customers.Address, "+;
     "City Customers.City, Country Customers.Country"
   Tables = "Customers"
However, the RecordSet will be created with its default CursorLocation and CursorType properties. Without changing these properties, the RecordSet is initially read-only; therefore, you will need to modify the oRS_Access method as follows:



   FUNCTION oRS_Access() as ADODB.RecordSet
     LOCAL loRS as ADODB.RecordSet
     IF VARTYPE(this.oRS)<>"O" THEN 
       this.oRS = NULL
       loRS = NEWOBJECT("ADODB.Recordset")
       IF VARTYPE(loRS)="O" THEN 
         loRS.CursorType= 3  && adOpenStatic 
         loRS.CursorLocation = 3  && adUseClient 
         loRS.LockType= 3  && adLockOptimistic
         this.oRS = loRS
       ENDIF 
     ENDIF 
     RETURN this.oRS
   ENDFUNC
With these additional settings for the RecordSet, the CursorAdapter can now handle automatic updates.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date