Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to handle RI in CursorAdapter?
Message
De
16/01/2004 03:33:11
 
 
À
15/01/2004 21:32:51
Czarina Joyce Villanueva
Innovision Systems International
Manila, Philippines
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Divers
Thread ID:
00866953
Message ID:
00867381
Vues:
23
>Aleksey,
>
>Look at my code below and tell me what's wrong or lacking?

Hi Czarina,

You are not fetching auto-generated value from the server.
Here is an example I have for Northwind.Categories table:
SET MULTILOCKS ON
SET DELETED ON
CLOSE DATABASES ALL 
CLEAR

oCA=CREATEOBJECT("CACategoriesODBC")
* Use ODBC connection to the Northwind database on the local SQL Server as a data source
oCA.DataSource=SQLSTRINGCONNECT("DRIVER=SQL Server;SERVER=(local);Trusted_Connection=Yes; DataBase=Northwind")


IF !oCA.AutoOpen()
	AERROR(errs)
	DISPLAY MEMORY like errs
	return
ENDIF

SELECT (oCA.Alias)
LIST

? "Add new category"
INSERT INTO (oCA.Alias) (Name) VALUES ("New category")
?[TABLEUPDATE(.F.)=],TABLEUPDATE(.F.)
LIST

* delete the new record from the table
GO bottom
DELETE
TABLEUPDATE(.F.)


SQLDISCONNECT(oCA.DataSource)
RETURN 


DEFINE CLASS CACategoriesODBC AS CursorAdapter
	Alias="CACategories"
	
	DataSourceType="ODBC"
	SelectCmd="select * from categories"
	CursorSchema="Id I, Name C(15), Descr M, Pict M"

	* set update properties
	Tables="Categories"
	KeyFieldList="Id"
	UpdatableFieldList="Name, Descr, Pict"
	UpdateNameList="Id Categories.CategoryId, Name Categories.CategoryName,"+;
		"Descr Categories.Description, Pict Categories.Picture"
	CompareMemo=.F.
	
	* Use ConversionFunc property to remove leading and trailing spaces from the Name field before sending 
	* its value to the server 
	ConversionFunc="Name AllTrim"
	
	* set conflict check type
	ConflictCheckType=3
	
	
	FUNCTION AutoOpen()
		NODEFAULT 
		RETURN this.CursorFill(.T.) && use schema 
	ENDFUNC
	
	PROCEDURE AfterInsert
		LPARAMETERS cFldState, lForce, cInsertCmd, lResult
		IF lResult=.T. && get new Id
			LOCAL currentArea
			currentArea=SELECT()
			
			* if insert is successful, update ID field with 
			* real value using @@IDENTITY variable from the server
			* (CategoryId is IDENTITY field)
			TRY
				IF 1=SQLEXEC(this.DataSource,"SELECT @@IDENTITY as CategoryId","IDRes")
					replace id with IDRes.CategoryId IN (This.Alias)
					USE IN IDRes
				ELSE
					AERROR(errs)
					DISPLAY MEMORY like errs
				ENDIF
			FINALLY
				SELECT (currentArea)
			ENDTRY	
		ENDIF
	ENDPROC 
ENDDEFINE
Thanks,
Aleksey.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform