VFP CursorAdapter how-to
(magyar verzió lentebb)

There's an SQL Express hosted database. The target is to handle the tables on the server as like local views, with a shared connection, via OLEDB. Creating a CursorAdapter (CA) is not a big deal, but it doesn't work as I expected. So there're two options. Create a class from a PRG or use the CA builder but customize it. Let's see the last.
  1. Create a CursorAdapter in a class library.
  2. Use the builder with a 'use connection settings in builder only' option to setup a table, using ADO type at the datasource type combolist.
  3. When you're ready with it, modify the datasource to 'use existing ADO Recordset' with the value of: this.oRS
  4. Create a new property with name: oRS, default value: NULL, with Access method
    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
  5. Add these three lines to the Init method before the last return expression:
    this.oRS.ActiveConnection = poConn
    this.DataSource=this.ors
    this.CursorFill()
  6. Define a public variable: poConn. It will be an object, to establish a connection to the SQL server. All the CAs will use this connection:
    PUBLIC poConn as ADODB.Connection
    poConn = NEWOBJECT("ADODB.Connection")
    IF VARTYPE(poConn)="O" THEN
    poConn.ConnectionString = "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PPS;Data Source=(local)\sqlexpress"
    poConn.OPEN()
    ENDIF
How to use it (continuing from point 6).
I created the CA in a class library named SQL.VCX. I have two sample tables in the database: ini and customer. So let§s see how can we access the data.
SET CLASSLIB TO classes\sql.vcx

PUBLIC poIni as CursorAdapter
PUBLIC poCustomer as CursorAdapter
poIni = CREATEOBJECT('caIni')
poCustomer = CREATEOBJECT('caCustomer')

*** at this point I have two open cursors with the aliases I setup in the builder

*** when I finished there's a task: to clean up ;)
poCustomer.Destroy
poCustomer = null
poIni.Destroy
poIni = null
poConn.Close()

HUNGARIAN version
Saját CursorAdapter létrehozása, ami a poConn nevű OLEDB kapcsolatot tartalmazó objektumot használja adatbázis eléréshez
1. új CursorAdapter-t kell létrehozni valahol.
2. beállítani mindent egy ideiglenes kapcsolattal a Builder-ben
3. A kapcsolatot módosítani kell erre: this.oRS
4. létre kell hozni egy új property-t, aminek a default value-je NULL, és van Access method-ja, és a neve: oRS
5. Az Access method tartalmazza ezt:
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
6. Az Init method-hoz pedig ezt még a végső return előtt hozzá kell adni:
this.oRS.ActiveConnection = poConn
this.DataSource=this.ors
this.CursorFill()

A poConn obejtum:
PUBLIC poConn as ADODB.Connection
poConn = NEWOBJECT("ADODB.Connection")
IF VARTYPE(poConn)="O" THEN
poConn.ConnectionString = "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PPS;Data Source=(local)\sqlexpress"
poConn.OPEN()
ENDIF

Hogyan használjuk, amit csináltunk.
Az egész egyben. Az SQL kiszolgáló a helyi gépen van, az adatbázis neve, amiben a két példa tábla van: PPS. A két tábla amúgy az ini és a customer, de ezt amúgy is a CursorAdapter definiálja.

PUBLIC poConn as ADODB.Connection
poConn = NEWOBJECT("ADODB.Connection")
IF VARTYPE(poConn)="O" THEN
poConn.ConnectionString = "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PPS;Data Source=(local)\sqlexpress"
poConn.OPEN()
ENDIF

SET CLASSLIB TO classes\sql.vcx

PUBLIC poIni as CursorAdapter
PUBLIC poCustomer as CursorAdapter
poIni = CREATEOBJECT('caIni')
poCustomer = CREATEOBJECT('caCustomer')

*** Ennél a pontnál két nyitott cursor is van, lehet az adatokkal dolgozni. Ha vége, nem kell már, akkor illik kitakarítani magunk után.

poCustomer.Destroy
poCustomer = null
poIni.Destroy
poIni = null
poConn.Close()

Megjegyzések

Népszerű bejegyzések ezen a blogon

Asus WL-700ge firmware upgrade

Gigaset C470IP setup