The Connect method opens a connection to the data source. This is typically used in the OpenDataSource script of a Database object. OpenDataSource isn't normally used, but is useful if you need to change what happens when Stonefield Query connects to a data source.

If Stonefield Query fails to connect the data source, the ErrorMessage property contains the error message from the database engine. The error is also logged in the diagnostic file.

Syntax

Connect() as Boolean

Parameters
None

Return Value
True if the connection was successfully made, False if not.

Example
This example, taken from an OpenDataSource script, opens a non-main database on the same server as the main database.

Visual FoxPro

lparameters toApplication as SQApplication, toDatabase as Database, ;
  tcDataSource
local loDatabase as Database, loDataSource as ODBCDataSource, ;
  loNewDataSource as ODBCDataSource
loDatabase      = toApplication.DataEngine.Databases.GetMainDatabase()
loDataSource    = loDatabase.CurrentDataSource
loNewDataSource = toDatabase.DataSources.Item(1)
loNewDataSource.Server   = loDataSource.Server
loNewDataSource.UserName = loDataSource.UserName
loNewDataSource.Password = loDataSource.Password
loNewDataSource.Disconnect()
return loNewDataSource.Connect()

VBScript

function Main(Application, Database, DataSource)
dim MainDatabase, MainDataSource, NewDataSource
set MainDatabase   = Application.DataEngine.Databases.GetMainDatabase()
set MainDataSource = MainDatabase.CurrentDataSource
set NewDataSource  = Database.DataSources.Item(1)
NewDataSource.Server   = MainDataSource.Server
NewDataSource.UserName = MainDataSource.UserName
NewDataSource.Password = MainDataSource.Password
NewDataSource.Disconnect()
Main = NewDataSource.Connect()
end function

JavaScript

function Main(Application, Database, DataSource) {
var MainDatabase   = Application.DataEngine.Databases.GetMainDatabase() ;
var MainDataSource = MainDatabase.CurrentDataSource ;
var NewDataSource  = Database.DataSources.Item(1) ;
NewDataSource.Server   = MainDataSource.Server ;
NewDataSource.UserName = MainDataSource.UserName ;
NewDataSource.Password = MainDataSource.Password ;
NewDataSource.Disconnect() ;
return NewDataSource.Connect() ;
}

C#

public static bool northwind_OpenDataSource(SFQApplication sfqApplication, 
  Database database, string dataSource)
{	
  database mainDatabase   = sfqApplication.DataEngine.Databases.GetMainDatabase();
  DataSource mainDataSource = mainDatabase.CurrentDataSource;
  DataSource newDataSource  = database.DataSources.Item(0);
  newDataSource.Server   = mainDataSource.Server;
  newDataSource.UserName = mainDataSource.UserName;
  newDataSource.Password = mainDataSource.Password;
  newDataSource.Disconnect() ;
  return newDataSource.Connect() ;
}

VB.NET

public shared function northwind_OpenDataSource(sfqApplication as SFQApplication,
  database as Database, dataSource as string) as Boolean
  dim mainDatabase as Database = sfqApplication.DataEngine.Databases.GetMainDatabase()
  dim mainDataSource as DataSource  = mainDatabase.CurrentDataSource
  dim newDataSource as DataSource  = database.DataSources.Item(0)
  newDataSource.Server   = mainDataSource.Server
  newDataSource.UserName = mainDataSource.UserName
  newDataSource.Password = mainDataSource.Password
  newDataSource.Disconnect()
  return newDataSource.Connect()
End Function

This code calls the GetMainDatabase method of the Databases collection of the DataEngine object of the passed Application object to return a Database object containing properties for the main database. It then uses the CurrentDataSource property of the main database object to get a reference to the DataSource object for the current data source. The database that this is the script for has only one data source object in its DataSources collection (Stonefield Query sets this up automatically), so a reference to that object is put into the NewDataSource variable. Then the Server, UserName, and Password properties of that DataSource object are set to the appropriate values from the main database's current DataSource object. The data source's Disconnect method is then called to ensure any former connection is closed and the Connect method is called to connect to the data source.

See also

Database Object | Disconnect | OpenDataSource | Select