MasteringVFP/14/2
出自VFP Wiki
目錄 |
從COM到MTS
介紹
VFP 也能撰寫支援 MTS 伺服器的元件,最主要你得先生成 MTXAS.APPSERVER 物件,然後調用此物件來進行交易(Transaction)。
#DEFINE MTX_CLASS "MTXAS.APPSERVER.1" LOCAL oMTX,oContext oMtx = CREATEOBJECT(MTX_CLASS) oContext = oMtx.GetObjectContext()
在交易的最後記得呼叫 SetComplete() 表明交易完成,若失敗,則調用 SetAbort() 表明交易中止。另外還可以使用 IsInTransaction() 來測試目前是否在交易中。
範例
DEFINE CLASS mts2 AS Custom OLEPUBLIC MyColor = "Green" PROCEDURE InUsa (tcCustID) LOCAL llInUSA,oMTX,oContext oMtx = CreateObject("MTXAS.APPSERVER.1") oContext = oMtx.GetObjectContext() llInUSA = .F. USE CUSTOMER AGAIN SHARED LOCATE FOR UPPER(cust_id) == UPPER(tcCustID) IF FOUND() llInUSA = (ATC("USA",country)#0) ENDIF oContext.SetComplete() RETURN llInUSA ENDPROC ENDDEFINE
怎麼用程式來安裝呢??
#DEFINE MTS_CATALOG "MTSAdmin.Catalog.1" #DEFINE ERR_NOACTION_LOC "No action taken." LOCAL oCatalog,oPackages,oUtil,i,j,oComps,nPoslcPackage LOCAL lPackageExists,oCompRef LOCAL oProject,lnServers,laProgIds,lcSaveExact,oPackageRef,lctrans lcPackage = ALLTRIM(THIS.cboPackages.DisplayValue) lPackageExists = .f. SELECT mtssvrs LOCATE FOR include IF !FOUND() OR EMPTY(lcPackage) MESSAGEBOX(ERR_NOACTION_LOC) RETURN ENDIF THIS.Hide oCatalog = CreateObject(MTS_CATALOG) oPackages = oCatalog.GetCollection("Packages") oPackages.Populate() FOR i = 0 TO oPackages.Count-1 IF UPPER(oPackages.Item(m.i).Name) == UPPER(lcPackage) oPackageRef = oPackages.Item(m.i) lPackageExists=.T. EXIT ENDIF ENDFOR IF !lPackageExists &&creating new package oPackageRef = oPackages.Add oPackageRef.Value("Name") = lcPackage oPackages.SaveChanges ENDIF oComps = oPackages.GetCollection("ComponentsInPackage",; oPackageRef.Key) oUtil = oComps.GetUtilInterface SCAN FOR include oUtil.ImportComponentByName(ALLTRIM(progid)) ENDSCAN oPackages.SaveChanges() oComps.Populate() SCAN FOR include DO CASE CASE trans = 1 lctrans = "Supported" CASE trans = 2 lctrans = "Required" CASE trans = 3 lctrans = "Requires New" OTHERWISE lctrans = "Not Supported" ENDCASE FOR j = 0 TO oComps.Count-1 IF oComps.Item(m.j).Value("ProgID")=ALLTRIM(progid) oCompRef = oComps.Item(m.j) oCompRef.Value("Transaction") = lctrans oCompRef.Value("SecurityEnabled") = ; IIF(THIS.chkSecurity.Value,"Y","N") ENDIF ENDFOR ENDSCAN oComps.SaveChanges() oPackages.SaveChanges()