Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ADO, CURSORADAPATER and Parameters
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows Server 2003
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00973221
Message ID:
00973230
Views:
15
Hi Rick,

I recently noticed that the ?VariableName Syntax is not working VFP9 but does work in VFP8. I posted recently in Thread 944961 and 972845 Where you can see some examples that previously worked in VFP8.

I made this function to handle the problem as I couldnt wait for an Answer maybe you can use something from it where I setup the commmand object and the parameters. Its very "fresh" but works for me.

Notes:
-you may need to add more parameters if you for example search on dates (i dont)
-the lEditable part is specific to the way I do it you probably dont need it.
-oConnection is a public ADO connection

Example Call:
lcUserName="DB2ADMIN"
FetchCursor("select * from shared.users where username<>?lcUserName","users",.T.)
FUNCTION FetchCursor
LPARAMETERS lcSQL,lcCursorName,lEditable,lShowErrors,lAsync
LOCAL loCursor as CursorAdapter
LOCAL loRecordSet As adodb.recordset
LOCAL loCommand As adodb.command
LOCAL llok,lnOptions,lcRestSQL,lnNumQs,lcParameter,lxValue,lcName,lnType,lcType,lnSize
LOCAL loCursor As CursorAdapter 

	IF lEditable
		lcCursorAdaptorName="go"+lcCursorName
		PUBLIC &lcCursorAdaptorName
	ENDIF	

	IF PCOUNT()=2
		lEditable=.F.
		lShowErrors=.T.
		lAsync=.T.
	ENDIF
	
	IF PCOUNT()=3
		lShowErrors=.T.
		lAsync=.F.
	ENDIF
	
	IF !EMPTY(lcCursorName) AND USED(lcCursorName)
		USE IN (lcCursorName)
	ENDIF
	

	loRecordSet = CREATEOBJECT("adodb.recordset")
	loCommand = CREATEOBJECT("adodb.Command")
	
	IF lAsync
		lnOptions=0x00000020	&&ADRUNASYNC
	ELSE
		lnOptions=0
	ENDIF
	
	WITH loCommand
		.activeconnection=oConnection
		
		lcRestSQL=SUBSTR(lcSQL,AT('?',lcSQL))
		lnNumQs=OCCURS("?",lcRestSQL)
	
		FOR i=1 TO lnNumQs
			IF ATC(' ',lcRestSQL)=0
				lcParameter=lcRestSQL
			ELSE
				lcParameter=SUBSTRC(lcRestSQL,1,ATC(' ',lcRestSQL))
			ENDIF
			
			lcParameter=STRTRAN(lcParameter,'?','')
			lcRestSQL=SUBSTR(lcRestSQL,AT(lcParameter,lcRestSQL))
			
			IF !EMPTY(lcParameter)
				lcName="Param"+ALLTRIM(STR(i))
				lxValue=EVALUATE(lcParameter)
				
				lcType=TYPE("lxValue")
				DO CASE
					CASE lCType="C"
						lnType=200
						lnSize=LEN(lxValue)
					CASE lCType="N"
						lnType=3	
						lnSize=20
				ENDCASE
				
				IF lnSize=0
					lnSize=10
				ENDIF
				.Parameters.Append(.CreateParameter(lcName,lnType,0x0001,lnSize,lxValue))
				**remove from the sql
				lcSQL=STRTRAN(lcSQL,'?'+lcParameter,'? ')
				lcRestSQL=SUBSTR(lcRestSQL,AT('?',lcRestSQL))
			ENDIF
			
			
		ENDFOR
		.commandtext=lcSQL
	ENDWITH

	
	loRecordset = loCommand.Execute(,,lnOptions)
	IF lEditable
		
		&lcCursorAdaptorName = CREATEOBJECT('CURSORADAPTER')
	
		WITH &lcCursorAdaptorName
			.Alias=lcCursorName
			.DataSourceType="ADO"
			.DataSource=loRecordset
		ENDWITH	
		
		llok=.T.
		IF !&lcCursorAdaptorName..CursorFill(,,,loRecordset)
	       IF lShowErrors
	       	 AERROR(laErrors)
	         MESSAGEBOX(laErrors[2],0+16,"QuestPDM - FetchCursor")
	       ENDIF  
	       llok=.F.
	    ENDIF
	    
	ELSE
		loCursor = CREATEOBJECT('CURSORADAPTER')
		loCursor.Alias=lcCursorName
		loCursor.DataSourceType="ADO"
		
		llok=.T.
		
		IF !loCursor.CursorFill(,,,loRecordset)
	       IF lShowErrors
	       	 AERROR(laErrors)
	         MESSAGEBOX(laErrors[2],0+16,"QuestPDM - FetchCursor")
	       ENDIF  
	       llok=.F.
	    ENDIF
	    
    	IF llok
			loCursor.CursorDetach()
		ENDIF
	ENDIF

	IF lEditable
		CURSORSETPROP("buffering",3)
	ENDIF
RETURN llok
Previous
Reply
Map
View

Click here to load this message in the networking platform