Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ConversionFunc
Message
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
00812237
Message ID:
00814043
Views:
122
>Hi
>
>Cursroradapter Datasource is Dataenvironment Datasource which is ADO and Connecting to sql server using ODBC, i've not changed any property of updatecmddatasource so its default

The code below is a complete example using ADO. It creates a CA for the Customer table in the Northwind MSDE database, but you can modify it to connect to any table in your own SQL database. You should try creating ADO connection the way I do below instead of an ODBC connection.
*!* ADO CursorAdapter Example for the SQL Server Northwind sample database
*!*
RELEASE oCA
PUBLIC oCA

oCA = CREATEOBJECT('adpADO')
oCA.cCompanyName = 'M%'
oCA.lAutoGenerateSQL = .F.
oCA.lUseCursorSchema = .F.
oCA.GetSQLData()
CLEAR
IF oCA.lFilled
   LIST STRUCTURE
   BROWSE LAST NORMAL TITLE oCA.SelectCmd
   oCA.SaveData()
   IF oCA.lSaved
      MESSAGEBOX('Save worked.')
   ELSE
      MESSAGEBOX('Save failed.')
      ?
      ? 'Save Failed'
      ? oCA.aErrorInfo(1)
      ? oCA.aErrorInfo(2)
      _CLIPTEXT = oCA.aErrorInfo(2)
   ENDIF
ELSE
   MESSAGEBOX('CursorFill Failed')
   ?
   ? 'CursorFill Failed'
   ? oCA.aErrorInfo(1)
   ? oCA.aErrorInfo(2)
   _CLIPTEXT = oCA.aErrorInfo(2)
ENDIF
oCA.cCompanyName = 'T%'
oCA.NewData()
IF oCA.lFilled
   CLEAR
   LIST STRUCTURE
   BROWSE LAST NORMAL TITLE oCA.SelectCmd
   oCA.SaveData()
   IF oCA.lSaved
      MESSAGEBOX('Save worked.')
   ELSE
      MESSAGEBOX('Save failed.')
      ?
      ? 'Second save failed'
      ? oCA.aErrorInfo(1)
      ? oCA.aErrorInfo(2)
   ENDIF
ELSE
   MESSAGEBOX('CursorRefresh Failed')
   ?
   ? 'CursorRefresh failed'
   ? oCA.aErrorInfo(1)
   ? oCA.aErrorInfo(2)
ENDIF
*!* SUSPEND
oCA.RELEASE()
RETURN

DEFINE CLASS adpADO AS CURSORADAPTER

   lSelectID = .F.
   cCompanyName = [M%]
   cCustomerID = [M%]

   *!* Change lUseCursorSchema to .T. to create a cursor according to the CursorSchema property
   lUseCursorSchema = .F.

   *!* Set the following property to .F. if you want the adapter to use your own SQL found
   *!*   in the InsertCmd, UpdateCmd and DeleteCmd properties. See Assign method below.
   *!*   Set it to .T. if you want VFP to auto-generate the SQL commands.
   lAutoGenerateSQL = .F.

   ALIAS = [crsCustomers]
   CURSORSCHEMA = [CustomerID C(5),CompanyName C(40),ContactName C(30),ContactTitle C(30),] ;
      + [Address C(60),City C(15),Region C(15),PostalCode C(10),Country C(15),Phone C(24),] ;
      + [Fax C(24)]
   BUFFERMODEOVERRIDE = 5
   DATASOURCETYPE = [ADO]
   ALLOWUPDATE = .T.
   ALLOWDELETE = .T.
   ALLOWINSERT = .T.

   WHERETYPE = 3

   lFilled = .F.
   DIMENSION aErrorInfo(1)
   DIMENSION aUpdateInfo(1)
   nError = 0
   lSaved = .F.

   PROTECTED PROCEDURE INIT
      CLEAR
      THIS.ADDPROPERTY('oConn', NEWOBJECT('ADODB.Connection'))
      THIS.ADDPROPERTY('oRS', NEWOBJECT('ADODB.Recordset'))
      THIS.ADDPROPERTY('oCommand', NEWOBJECT('ADODB.Command'))

      WITH THIS.oConn
         .CursorLocation = 3
         .ConnectionString = ;
              [Provider=SQLOLEDB.1;Integrated Security=SSPI;] ;
            + [Persist Security Info=False;] ;
            + [Initial Catalog=Northwind;Data Source=localhost]
         .OPEN()
      ENDWITH
      THIS.oCommand.ActiveConnection = THIS.oConn
      WITH THIS.oRS
         .ActiveConnection = THIS.oConn
         .CursorType       = 1  && adOpenKeyset
         .CursorLocation   = 3  && adUseClient
         .LockType         = 3  && adLockOptimistic
      ENDWITH
      THIS.DATASOURCE = THIS.oRS
      THIS.UPDATECMDDATASOURCETYPE = [ADO]
      THIS.UPDATECMDDATASOURCE     = THIS.oCommand
      THIS.DELETECMDDATASOURCETYPE = [ADO]
      THIS.DELETECMDDATASOURCE     = THIS.oCommand
      THIS.INSERTCMDDATASOURCETYPE = [ADO]
      THIS.INSERTCMDDATASOURCE     = THIS.oCommand
      THIS.lAutoGenerateSQL = .F.  && needed to initially fire the Assign method for this property
   ENDPROC
   PROCEDURE GetSQLData
      IF THIS.lSelectID
         THIS.SelectCmd = [SELECT * FROM Customers WHERE CustomerID LIKE ?cCustomerID ORDER BY CompanyName]
      ELSE
         THIS.SelectCmd = [SELECT * FROM Customers WHERE CompanyName LIKE ?cCompany ORDER BY CompanyName]
      ENDIF
      THIS.ResetError()
      PRIVATE cCompany, cCustomerID
      cCompany = THIS.cCompanyName
      cCustomerID = THIS.cCustomerID
      THIS.lFilled = THIS.CURSORFILL(THIS.lUseCursorSchema, .F., -1, THIS.oCommand)
      IF NOT THIS.lFilled
         THIS.nError = AERROR(THIS.aErrorInfo)
      ENDIF
   ENDPROC
   PROCEDURE SaveData
      LOCAL laInfo(1), lnRecord
      THIS.ResetError()
      THIS.lSaved = .T.
      SELECT crsCustomers
      THIS.lSaved = TABLEUPDATE(1, .F., 'crsCustomers')
      IF NOT THIS.lSaved
         THIS.nError = AERROR(THIS.aErrorInfo)
      ENDIF
   ENDPROC
   PROCEDURE NewData
      PRIVATE cCompany, cCustomerID
      cCompany = THIS.cCompanyName
      cCustomerID = THIS.cCustomerID
      THIS.lFilled = THIS.CURSORREFRESH()
      IF NOT THIS.lFilled
         THIS.nError = AERROR(THIS.aErrorInfo)
      ENDIF
   ENDPROC
   PROTECTED PROCEDURE ResetError
      THIS.nError = 0
      THIS.aErrorInfo = []
   ENDPROC
   PROTECTED PROCEDURE DESTROY
      THIS.oRS = .NULL.
      THIS.oConn = .NULL.
      THIS.oCommand = .NULL.
   ENDPROC
   PROCEDURE RELEASE
      RELEASE THIS
   ENDPROC

   PROTECTED PROCEDURE lAutoGenerateSQL_Assign
      LPARAMETERS tlAuto
      IF VARTYPE(tlAuto) <> "L"
         RETURN .F.
      ENDIF
      THIS.lAutoGenerateSQL = tlAuto
      IF tlAuto
         WITH THIS
            .KEYFIELDLIST = [CustomerID]
            .TABLES = [Customers]
            .UPDATECMD = []
            .INSERTCMD = []
            .DELETECMD = []
            .UPDATABLEFIELDLIST = [CompanyName,] ;
               + [ContactName,] ;
               + [ContactTitle,] ;
               + [Address,] ;
               + [City,] ;
               + [Region,] ;
               + [PostalCode,] ;
               + [Country,] ;
               + [Phone,] ;
               + [Fax]
            .UPDATENAMELIST = [CustomerID Customers.CustomerID,] ;
               + [CompanyName Customers.CompanyName,] ;
               + [ContactName Customers.ContactName,] ;
               + [Contacttitle Customers.ContactTitle,] ;
               + [Address Customers.Address,] ;
               + [City Customers.City,] ;
               + [Region Customers.Region,] ;
               + [Postalcode Customers.PostalCode,] ;
               + [Country Customers.Country,] ;
               + [Phone Customers.Phone,] ;
               + [Fax Customers.Fax]
            .CONVERSIONFUNC = [CompanyName RTRIM,] ;
               + [ContactName RTRIM,] ;
               + [ContactTitle RTRIM,] ;
               + [Address RTRIM,] ;
               + [City RTRIM,] ;
               + [Region RTRIM,] ;
               + [PostalCode RTRIM,] ;
               + [Country RTRIM,] ;
               + [Phone RTRIM,] ;
               + [Fax RTRIM]
         ENDWITH
      ELSE
         WITH THIS
            .KEYFIELDLIST = []
            .TABLES = []
            .UPDATABLEFIELDLIST = []
            .UPDATENAMELIST = []
            .CONVERSIONFUNC = []
            .UPDATECMD = [UPDATE Customers SET ] ;
               + [CompanyName = ?RTRIM(crsCustomers.CompanyName), ] ;
               + [ContactName = ?RTRIM(crsCustomers.ContactName), ] ;
               + [ContactTitle = ?RTRIM(crsCustomers.ContactTitle), ] ;
               + [Address = ?RTRIM(crsCustomers.Address), ] ;
               + [City = ?RTRIM(crsCustomers.City), ] ;
               + [Region = ?RTRIM(crsCustomers.Region),] ;
               + [PostalCode = ?crsCustomers.PostalCode, ] ;
               + [Country = ?RTRIM(crsCustomers.Country), ] ;
               + [Phone = ?RTRIM(crsCustomers.Phone), ] ;
               + [Fax = ?RTRIM(crsCustomers.Fax) ] ;
               + [WHERE CustomerID = ?crsCustomers.CustomerID]
            .INSERTCMD = [INSERT INTO Customers ( ] ;
               + [CustomerID, ] ;
               + [CompanyName, ] ;
               + [ContactName, ] ;
               + [ContactTitle, ] ;
               + [Address, ] ;
               + [City, ] ;
               + [Region, ] ;
               + [PostalCode, ] ;
               + [Country, ] ;
               + [Phone, ] ;
               + [Fax ] ;
               + [) ] ;
               + [values ( ] ;
               + [?RTRIM(crsCustomers.CustomerID), ] ;
               + [?RTRIM(crsCustomers.CompanyName), ] ;
               + [?RTRIM(crsCustomers.ContactName), ] ;
               + [?RTRIM(crsCustomers.ContactTitle), ] ;
               + [?RTRIM(crsCustomers.Address), ] ;
               + [?RTRIM(crsCustomers.City), ] ;
               + [?RTRIM(crsCustomers.Region), ] ;
               + [?RTRIM(crsCustomers.PostalCode), ] ;
               + [?RTRIM(crsCustomers.Country), ] ;
               + [?RTRIM(crsCustomers.Phone), ] ;
               + [?RTRIM(crsCustomers.Fax) ] ;
               + [)]
            .DELETECMD = [DELETE FROM Customers WHERE CustomerID = ?crsCustomers.CustomerID]
         ENDWITH
      ENDIF
   ENDPROC
ENDDEFINE
Mark McCasland
Midlothian, TX USA
Previous
Reply
Map
View

Click here to load this message in the networking platform