FoxDataObjects

Object-Relational Mapping Tool for Visual FoxPro®

 Connect  Method

 

 

Connects the session to a data server.

 Session.Connect( [oDataSource] )

 

Parameters

 

oDataSource

 

A fdoDataSource subclass instance. Specifies the data source object to be used for session connection.

 

Return Value

 

Logical

Remarks

 

This method try to establish a connection to a data store. It returns True (.T.) if connection is established. otherwise it returns False (.F.), in such case, the Errors collection contains detailed information on what went wrong.

Before attempting to connect to a data store, the Session.BeforeConnect event is fired. If this event returns a True value (.T.) connection attempt continues, otherwise connection is aborted and Session.Connect returns False.

In order to connect to a data store, Session.Connect uses a DataSource object. A datasource object is an object instance of one of the fdoDataSource subclasses provided with FDO that supplies all the information needed to establish a connection. For a complete reference on DataSources please see the fdoDataSource class reference.

A DataSource object can be passed as a parameter to the Session.Connect method. When no datasource object has been received, Session.Connect will check the Session.DataSourceLookupMode property to know how to search for a valid datasource object to use for the connection.

The next table summarizes the DataSourceLookupMode values:

Setting Description
0 DSLOOKUPMODE_DISABLED A valid datasource object must be passed as a parameter to the Session.Connect method in order to connect to a data store.
1 DSLOOKUPMODE_SCHEMAFILE Session.Connect method will search on the active mapping schema for a valid Datasource definition marked as default.
2 DSLOOKUPMODE_SERVER Session.Connect method will use the Server.DefaultDataSource object.
3 DSLOOKUPMODE_SCHEMAFILEANDSERVER Session.Connect method will first try to find a valid datasource definition marked as default on the active mapping schema, if no one is found, it will use the Server.DefaultDataSource object.

If no valid datasource object is found, Session.Connect returns False.

Once Session.Connect has a valid DataSource object, the DataSource.AccessType property instructs the persistence engine either to connect to a Visual FoxPro native Database (direct file access to DBC file) or to connect to a remote data store by using ODBC.

If the datasource object AccessType is 1 (ODBC), Session.Connect gets the ODBC connection string from the datasource object by calling the DataSource.GetConnectionString method, and tries to connect via the SqlStringConnect VFP function. Except when the DataSource.ODBC_ConnectionHandlerSuppliedByClient values is .T., where Session.Connect skips the whole connection procedure and uses an existing ODBC handler value previously stored on Session.ODBC_ConnectionHandler.

The datasource object used for connection is stored on the Session.DataSource property and is later used for automatic reconnections.

If connection succeeds, then Session.AfterConnected event fires, otherwise, the Session.AfterConnectionFailure event fires.

Session.Connect returns .T. if connection succeeds or false if connection fails.

 

Example

SET PROCEDURE TO FDO ADDITIVE
oServer=CREATEOBJECT("fdoServer")
 

oServer.ConnectedSessionsOnly=.F.
 

oSession = oServer.NewSession("MyApp.fdo")

 

oDS=oServer.NewDataSource("MySQL")
oDS.Database_Name=”Contacts”
oDS.ODBC_SRV="192.168.0.5"
oDS.ODBC_PORT=3307

 

IF oSession.Connect(oDS)

   IF oSession.EnsureDatabaseStatus(4)

      ? "Session connected, database selected and database schema is UpToDate"

   ELSE

      oSession.Disconnect

      ? "Session disconnected. Database schema is out of date and update failed."

      ? oSession.Errors.ToString()

   ENDIF

ELSE

   ? "Cannot connect to datasource"

   ? oSession.Errors.ToString()

ENDIF

 

The sample shows how to get a disconnected session by setting the Server.ConnectedSessionsOnly property to false. Then a DataSource object is built for a remote MySQL database server using the "Contacts" database.

The Session.Connect method is called with the datasource object passed as a parameter.

If connection succeed, the Session.EnsureDataBaseStatus method will open the connection, check for the database, select the database and check for database schema version. If necessary, it will upgrade the database schema. (Note the CheckLevelStatus passed as a parameter is 4 CHECK_DBINUSEANDTABLESUPTODATE ). In case the database schema cannot be upgraded, the connection is closed using Session.Disconnect.

 

Example

SET PROCEDURE TO fdo ADDITIVE

 

oServer=CREATEOBJECT("fdoServer")

oServer.ConnectedSessionsOnly=.F.

 

oSession=oServer.NewSession("MyMappingSchema.FDO")

 

oDataSource=oServer.NewDataSource("MsSQLServer")

oDataSource.ODBC_ConnectionHandlerSuppliedByClient=.T.

 

oSession.ODBC_ConnectionHandler=nConn

IF oSession.Connect(oDataSource)

   ? "ODBC Handler",oSession.ODBC_ConnectionHandler

   oSession.Disconnect

ENDIF

 

This sample creates a DataSource object for Ms SQL Server, connects using an existing ODBC connection handler stored on nConn.
 

See Also

fdoDataSource class | Session.DataSourceLookupMode property | Session.ConnectionMode property | Session.ConnectionLifeTime property | Session.Disconnect method | Server.NewSession method | Server.ConnectedSessionsOnly property | Session.BeforeConnect event | Session.AfterConnected event | Session.AfterConnectionFailure event | Session.MappingSchema.GetDataSource method | DataSource.ODBC_ConnectionHandlerProvidedByClient property | Session.ODBC_ConnectionHandler property
Applies to: Session object

Send feedback on this topic to RunAhead Technologies

For Technical support and product issues please contact us at support@foxdataobjects.com or visit http://www.foxdataobjects.com

Copyright (c) 2000-2005 RunAhead Technologies