Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Data entry with MS SQL
Message
From
03/06/2003 05:05:15
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
03/06/2003 01:57:49
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00795534
Message ID:
00795557
Views:
16
>I am relatively new to using VFP7 with SQL server. I have expermented with both Remote view and SQL pass through. I am having trouble creating a form entering data then saving is to the SQL Server without going through alot of steps. The database has over 2,000,000 rows so obviously I have to be careful not to down load it to a view. I have tried remote view and a form with a data session with nodata = .t. but then I am not sure how to add a row and save data. Does anyone have some sample code or ideas.
>
>Thanks
>
>Tom

Tom,
Views are row buffered by default.
With an RV (nodataonload=.t.) :
To add a new record you'd do an insert and tableupdate (implicit or explicit). If your view has default values set you should be comfortable. Be carefull about primary key value. It might be an IDENTITY COLUMN (or ROWGUID COLUMN). It's important who generates it , you or SQL server, writable or not. ie: With IDENTITY SQL server auto generates it and you can't write. You can't query it before the insertion occurs (tableupdate). If ROWGUID, either SQL server default value or your view's default might request it to be created before insertion occurs (IMHO best surrogate PK). In this case you could write it.
SPT gives you more control (and arguable performance) over RV. But it has problems too like setting the datatype (in RV it's easy, part of DBC proerties). Apart from datatype problem, you could use an SPT almost like an updatable RV.
Of these 2 technologies RV is easier for a beginner and you almost work with a native buffered table from your POV.
Then there is another, ADO. Working with ADO can be problematic for a newcomer + ADO itself is being queried since release of ADO.NET (and I feel ADO will diminish in near future).
Best of all worlds is VFP8 cursoradapter (and XMLAdapter).

I'm not going into much detail as this is a huge topic by itself. First try with samples like Northwind and Pubs databases. Just to wet your toes :

SPT (Error check is not done as this one was straight forward)
This one directly does update in code - pay attention how VFP memvars are used as parameters.
#Define SQLCONNECTION 	"Driver=SQL Server;Integrated Security=SSPI;"+;
  "Database=Pubs; Server=servername"
Local lnHandle
lnHandle = SQLSTRINGCONNECT(SQLCONNECTION)
SQLEXEC(lnHandle,'select * from Titles','crsTitles')
*Let's show what we have got
BROWSE TITLE 'Contents of Titles Before Update'

m.BookType = 'psychology'
m.Amt = 1 && 1 $ change in price

*Now we'll decrease price by m.Amt $ where type is m.BookType
*First let's check again this set before update

SQLEXEC(lnHandle,"select * from Titles where type = ?m.BookType",'crsTitles')
BROWSE TITLE 'Filtered Contents of Titles Before Update'
*OK now let's decrease the prices and update
SQLEXEC(lnHandle,"update Titles set price = price - ?m.Amt where type = ?m.BookType")
*Check what we've done
SQLEXEC(lnHandle,"select * from Titles where type = ?m.BookType",'crsTitles')
BROWSE TITLE 'Filtered Contents of Titles After Update - decrease'
* Restore prices
SQLEXEC(lnHandle,"update Titles set price = price + ?m.Amt where type = ?m.BookType")
*Check what we've done
SQLEXEC(lnHandle,"select * from Titles where type = ?m.BookType",'crsTitles')
* We're done disconnect
SQLDisconnect(lnHandle)
BROWSE TITLE 'Filtered Contents of Titles After Update - restore'

function CheckError
local ix, lcMsg
local array arrErr[1]
aerror(arrErr)
set textmerge to memvar lcMsg noshow
set textmerge on
for ix =1 to 6 && aError returns 7 elems -we need first 6 at most for ODBC
  \<<arrErr[ix]>>
endfor
set textmerge to
set textmerge off
messagebox(lcMsg)
SPT (This one is similar to RV). Promotes SPT to an updatable cursor (like any other local view)
'Integrated Security' and 'Trusted_Connection' are interchangeable as shown - assumed windows authentication in both SPTs :
m.csql = "SELECT EmployeeID, LastName, FirstName," + ;
		"Title, TitleOfCourtesy, BirthDate," + ;
		"Hiredate, Address, City, Region," + ;
		"PostalCode, Country, HomePhone," + ;
		"Extension, Photo, Notes," + ;
		"ReportsTo, PhotoPath FROM dbo.Employees"

lnHandle=Sqlstringconnect('DRIVER=SQL Server;SERVER=servername;'+;
    'DATABASE=Northwind;Trusted_Connection=Yes')
SQLExec(lnHandle,cSQL,'v_emp')

* These properties are set for you by VFP if you use RV
* With SPT you need to write yourself to make it an updatable cursor
CursorSetProp('KeyFieldList','Employeeid','v_emp')
CursorSetProp('WhereType',1,'v_emp')
CursorSetProp('Tables','employees','v_emp')

CursorSetProp("UpdateNameList", ;
  "employeeID    employees.employeeid,"+;
  "Firstname employees.FirstName,"+;
  "Lastname employees.Lastname,"+;
  "BirthDate employees.birthdate",'V_emp')

CursorSetProp('UpdatableFieldList',"Firstname,Lastname,BirthDate",'v_emp')
CursorSetProp('SendUpdates',.T.,'v_emp')
CursorSetProp('Buffering',5,'v_emp')

* Edit here
Browse title 'Edit Firstname,Lastname,BirthDate fields'

Tableupdate(2,.T.,'v_emp')

SQLExec(lnHandle,'select * from dbo.employees','afterupdate')
SQLDisconnect(lnHandle)
* Check updates
Select afterupdate
Browse 'New contents of employees'
And finally an ADO sample using datagrid (very simple one, no erro check validation etc) :
#Define OLEDBCONNECTSTRING 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=Northwind; Data Source=cetin\cetin'
oForm = Createobject('frmSample')
oForm.Show
Read Events

Define Class frmSample As Form
  Top = 0
  Left = 0
  Height = 400
  Width = 500
  DoCreate = .T.
  Caption = "Form1"
  Name = "Form1"

  Add Object myDataGrid As OleControl With ;
    Top = 10, ;
    Left = 10, ;
    Height = 380, ;
    Width = 480, ;
    OleClass = 'MSDataGridLib.DataGrid',;
    Name = "myDataGrid"

  Procedure Init
    #Define OLEDBCONNECTSTRING 'Provider=SQLOLEDB.1;Integrated Security=SSPI;'+;
      'Initial Catalog=Northwind; Data Source=servername'

    This.AddProperty('oConn',Createobject('ADODB.Connection'))
    This.AddProperty('oRS',Createobject('ADODB.RecordSet'))

    With This.oConn
      .ConnectionString = OLEDBCONNECTSTRING
      .Mode = 16
      .Open
    Endwith
    With This.oRs
      .ActiveConnection = This.oConn
      .Source = 'select * from customers'
      .CursorLocation= 3  && adUseClient
      .CursorType= 3
      .LockType= 4  && adLockBatchOptimistic
      .Open
    Endwith
    Local oRs As ADODB.Recordset

    With This.myDataGrid
      .Datasource=This.oRs
      .Caption = "Customers Table"
    Endwith
  Endproc

  Procedure QueryUnload
    If Messagebox('Save ?',4+32,'Save changes') = 6
      This.oRs.UpdateBatch
    Endif
    This.oConn.Close
    Clear Events
  Endproc
Enddefine
With VFP8 even ADO cursors are easy to cope with :)
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform