Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How To make the COnnection persistent?
Message
General information
Forum:
Visual FoxPro
Category:
Client/server
Miscellaneous
Thread ID:
00435483
Message ID:
00437233
Views:
18
2 cents...

SQLCONNECT() and views set up in a database container each use a connection handle. When you share a connection with views, every time the last open view closes, so does the connection. Here's a trick:

Define a dummy view that does nothing (something like SELECT id_field from anytable where id_field=-1). Open this view on startup and leave it open. You might want to make an invisible form or create a session object with a private data session and open the view there so that it won't get closed accidentally in the default data session. Now the connection is open and the other views your app uses will share it as needed. You can now use CURSORGETPROP() on the view to retrieve the ConnectHandle. Don't use SQLCONNECT(), use this handle instead. You now have one connection for both views and SQL pass through and it will remain open at all times reducing connect time delays.

You may also want to consider not using pre-defined remote views, they are slow and have some limitations. Ever try allowing the user to select a live or test database? You have to re-configure the DSN in the ODBC control panel because the view is built with one specific DSN.

Here's a sample of how to do it without pre-defined remote views:

SQLEXEC(oAppObj.nODBCConnection,;
'SELECT ID_FIELD, FIELD1, FIELD2 FROM MYTABLE', 'csrTemp')
MAKEVIEWUPDATABLE('csrTemp', 5) &&5=table buffering


MAKEVIEWUPDATABLE() only works on single table selects (so far). Basically it does this:
CURSORSETPROP('SendUpdates', .T. 'csrTemp')
CURSORSETPROP('Tables', 'MYDB.MYTABLE')
CURSORSETPROP('UpdateNameList', 'id_field, field1, field2', 'csrTemp')
CURSORSETPROP('KeyFieldList', 'id_field', 'csrTemp')
CURSORSETPROP('UpdatableFieldList', 'id_field, field1, field2', 'csrTemp')


*******************************************************************
PROCEDURE MAKEVIEWUPDATABLE()
LPARAMETERS tcTable, tnBufferMode
LOCAL llRetVal, lcTable, lnI, lcFieldList, lcUpdList

if vartype('tcTable')<>'C'
messagebox('Please specify a table name when calling '+program(), 0, 'Parameter error')
return .f.
endif

llRetVal=.t.
lcTable=alltrim(tcTable)
select (lcTable)
if !upper(alias())==upper(lcTable)
messagebox('Can not select view: '+lcTable, 0, "Error Making View Updatable")
return .f.
endif

if !CursorSetProp('Tables', 'dbo.'+lcTable) &&list all tables used in the view
messagebox("CursorSetProp('Tables', 'dbo.'+lcTable) failed."+chr(13)+chr(10)+;
"lcTable="+lcTable, 0, "Error Making View Updatable")
endif

lcUpdList=''
lcFieldList=''
for lnI=1 to fcount() &&make a list of all fields in the view
lcFieldList=lcFieldList+field(lnI)+','
lcUpdList=lcUpdList+field(lnI)+' dbo.'+lcTable+'.'+field(lnI)+','
endfor
&&trim off that last ","
lcFieldList=substr(lcFieldList, 1, len(lcFieldList)-1)
lcUpdList=substr(lcUpdList, 1, len(lcUpdList)-1)

if !CursorSetProp('UpdateNameList', lcUpdList)
messagebox("CursorSetProp('UpdateNameList', lcUpdList) failed."+chr(13)+chr(10)+;
"lcUpdList="+lcUpdList, 0, "Error Making View Updatable")
endif

SQLEXEC(oAppObj.nODBCConnection,;
"{call sp_helpindex('"+lcTName+"')}", 'csrIDX1')
select index_keys from csrIDX1 into cursor csrIDX2 where 'PRIMARY KEY' $ index_description
if !CursorSetProp('KeyFieldList', csrIDX2.index_keys)
messagebox("CursorSetProp('KeyFieldList', tcPriKey) failed."+chr(13)+chr(10)+;
"tcPriKey="+tcPriKey, 0, "Error Making View Updatable")
endif

if !CursorSetProp('UpdatableFieldList', lcFieldList)
messagebox("CursorSetProp('UpdatableFieldList', lcFieldList) failed."+chr(13)+chr(10)+;
"lcFieldList="+lcFieldList, 0, "Error Making View Updatable")
endif
if !CursorSetProp('SendUpdates', .T.)
messagebox("CursorSetProp('UpdatableFieldList', lcFieldList) failed.",;
0, "Error Making View Updatable")
endif

if vartype(tnBufferMode)<>'N' or !inlist(tnBufferMode, 3, 5)
tnBuffermode=3 &&default to optomistic row
endif
if !CursorSetProp('Buffering', tnBufferMode)
messagebox("CursorSetProp('Buffering', tnBufferMode) failed."+chr(13)+chr(10)+;
"tnBufferMode="+transform(tnBufferMode), 0, "Error Making View Updatable")
endif
ENDPROC
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform