Re: Cursor Adapter in DE Question

Author: Rafael Copquin

Posted: 2011-10-13 at 08:20:36

As requested, below are the methods necessary to create a cursor adapter

on the fly

use thus:

thisform.makeadapter('customers','curCustomers',[select * from customers])

As for authentication, I use SQL Server authentication

Connect through ODBC

local cConnString

cConnString=[DRIVER=SQL Server Native Client

10.0;SERVER=MAINSERVER\SQLEXPRESS;UID=sa;PWD=12345;APP=Microsoft Visual

FoxPro;WSID=INVOICINGPC;DATABASE=TESTDATABASE;LANGUAGE=English;]

thisform.nHandle = SQLStringConnect(cConnString )

BTW don't ever use the master data, you could screw things up badly.

Just create a test database, put some tables in it, fill them with data

and go on.

I strongly advise you to read the books from Hentzenwerke, Megafox....,

Internet Applications..... etc. That's where you get all the information

you need

Rafael Copquin

** makeadapter method

Lparameters tcTable,tcAlias,tcCmd

Local cSCH,cUFL,cUNL,cCmd

thisform.lOK = .T.

thisform.structure(tcTable)

cUFL = Thisform.cUFL && updatable field list

cUNL = Thisform.cUNL && update name list

cSCH = Thisform.cSCH && table schema

cCmd = tcCmd

thisform.lOK = .T.

Try

Use in Select(tcAlias)

If PemStatus(this,'oCA',5) = .T.

Removeproperty(Thisform,"oCA")

Endif

This.AddProperty("oCA")

This.oCA = Createobject("cursoradapter")

Catch To oErrors

Messagebox(" CursorAdapter object generation failed"+;

Chr(13)+oErrors.Message,16,"Attention",2000)

thisform.lOK = .F.

Finally

Endtry

If thisform.lOK = .F.

Return

Endif

If thisform.lOK = .T.

Try

With Thisform.oCA

.DataSourceType = "ODBC"

.DataSource = thisform.nHandle

.Alias = tcAlias

.Tables = tcTabla

.BufferModeOverride = 5

.KeyFieldList = thisform.cKeyField

.SendUpdates = .t.

.usetransactions = .f. && when set to .F., the CA does

not use transactions so you must set transactions to manual in the SQL

Server

.SelectCmd = cCmd

.UpdatableFieldList = cUFL

.UpdateNameList = cUNL

.CursorSchema = cSCH

.CursorFill()

Endwith

Select (thisform.oCA.alias)

Catch To oErrors

messagebox("Cursor generation

failed"+Chr(13)+oErrors.Message,16,'Attention')

thisform.lOK = .F.

Endtry

Endif

return

**************************************************

**structure method

lparameter cTable

Local cSch,cUNL,cUFL,cField,N

Store '' to cSch,cUNL,cUFL

Store 0 to nResult

thisform.cKeyField = thisform.getkeyfield(cTable)

** this is the VFP cursor structure generated when data from SQL Server

brought back

SQLColumns(thisform.nHandle,cTable,[FOXPRO],'curStructure')

Select curStructure

Scan all

cUNL = cUNL + field_name+[ ]+JustStem(cTable)+[.]+field_name+[,]

cUFL = cUFL + field_name+[,]

cSCH = cSCH + field_name+[ ]+field_type

Do case

Case InList(field_type,[C],[D],[I],[T])

cSCH = cSCH + [(] +Transform(field_len) +[)]

Case field_type = [N]

cSCH = cSCH + [(]

+Transform(field_len)+[,]+Transform(field_dec) +[)]

Case field_type = [M]

cSCH = cSCH + [(] +Transform(8000) +[)]

EndCase

cSCH = cSCH + [,]

EndScan

Use in Select('curStructure')

** remove comma from end of strings

thisform.cSCH = Substr(cSCH,1,Len(Alltrim(cSCH))-1) && schema

thisform.cUNL = Substr(cUNL,1,Len(Alltrim(cUNL))-1) && updatenamelist

thisform.cUFL = Substr(cUFL,1,Len(Alltrim(cUFL))-1) && updatablefieldlist

return

***********************************************************

**getkeyfield method

lparameters cTable

Local cField,N,cKeyField

Use in Select('curStructure')

** this allows you to know what is the key field directly from the SQL

Server data

SQLColumns(thisform.nHandle,cTable,[NATIVE],'curStructure')

Select curStructure

Locate for Lower(Alltrim(type_name)) ='int identity'

If Found()

cKeyField = Alltrim(column_name)

EndIf

Use in Select('curStructure')

Return cKeyField

El 13/10/2011 1:24, Ajoy Khaund escribió:

> Rafael,

>

> Are you using Windows or SQL Server Authentication?

> How are you connecting to the SQL Server data in the main form?

>

> Currently I am trying out with the master data which seems to be working.

> Can you send me an example of

> building CA in code. Currently I have saved a class for each table that I am

> working with.

>

> Thanks

>

> Ajoy Khaund

> akhaund@hotmail.com

>

>

>

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/4E96D794.8010105@fibertel.com.ar

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2011 Rafael Copquin