The Disconnect method closes a connection to the data source. This is typically used in the OpenDataSource script of a Database object prior to calling the Connect method to ensure any existing connection is closed before opening a new one.

Syntax

Disconnect() as Boolean

Parameters
None

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

Example
This example 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() ;
}

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
Connect | Database Object | OpenDataSource | Select