[ Pobierz całość w formacie PDF ]
.The ConnectionString property can also contain the name of a file containingthe connection parameters.Such a file has the same contents as the ConnectionStringproperty: one or more parameters, each with a value assignment and separated fromother parameters by a semi-colon.See the VCL help topic for the ConnectionStringproperty for a list of ADO-supported parameters.Once the connection information has been provided in the connection component,associate dataset and command components with the connection component.Do thisby assigning a reference to the connection component to each dataset or commandcomponent s Connection property.At design-time, select the desired connectioncomponent from the drop-down list for the Connection property in the ObjectInspector.At runtime, assign the reference to the Connection property.For example,the command below associates a TADODataSet component with a TADOConnectioncomponent.ADODataSet1->Connection = ADOConnection1;If you do not explicitly activate the connection by setting the connection component sConnected property to true, it will happen automatically when the first datasetcomponent is activated or the first time a command is executed with a commandcomponent.Accessing the connection objectUse the ConnectionObject property of TADOConnection to access the underlying ADOconnection object directly.Using this reference it is possible to access properties andcall methods of the underlying ADO Connection object from an application.Use of ConnectionObject to directly access the underlying ADO Connection objectrequires a good working knowledge of ADO objects in general and the ADOConnection object in specific.It is not recommended that you use the Connectionobject directly unless familiar with Connection object operations.Consult theMicrosoft Data Access SDK help for specific information on using ADO Connectionobjects.Activating and deactivating the connectionTo activate an ADO connection component, set the TADOConnection::Connectedproperty to true or call the TADOConnection::Open method.ADOConnection1->Connected = true;For the connection to be successful, the connection information provided in theTADOConnection::ConnectionString property must define a valid connection.Formore information on providing connection information, see Specifying theconnection on page 24-3.Activating an ADO connection component will trigger the OnWillConnect andOnConnectComplete events of the ADO connection component and execute handlersfor these events if they have been assigned.24-4 Dev el oper s Gui deCo n n e c t i n g t o ADO d a t a s t o r e sIf a connection component has not already been activated, it will automatically beactivated if an associated dataset or command component is enabled.Datasetcomponents cause this when they are activated.Command components do this whena command is executed.For information on associating dataset components with aconnection component, see Connecting to a data store using TADOConnection onpage 24-3.To deactivate an ADO connection component, either set its Active property to false orcall its Close method.ADOConnection1->Close();Four things happen when a connection component is deactivated, using either theActive property or the Close method:1 The TADOConnection::OnDisconnect event fires.2 The handler for the OnDisconnect event executes (if one is assigned).3 The TADOConnection component is deactivated.4 Any associated ADO command or dataset components are deactivated.Determining what a connection component is doingAt any time during the existence of a TADOConnection component, query its Stateproperty to determine what action, if any, in which the connection component iscurrently engaged.A TObjectStates value of stClosed in the TADOConnection::State property indicates thatthe connection object is currently inactive.The TADOConnection::Connected propertycontains a value of false and no associated command or dataset components areactive.A value of stOpen indicates that the connection component is active.A connectionwith an ADO data store has been successfully established, its Connected propertycontains a value of true, and any one or more associated command or datasetcomponents might be active.A value of stConnecting indicates the connection component is currently attemptingto establish a connection to the ADO data store specified in theTADOConnection::ConnectionString property.While still in this state, the Cancelmethod may be called to abort the connection attempt.Fine-tuning a connectionWhen a TADOConnection component is used to make the connection to a data storefor an application s ADO command and dataset components, you have a greaterdegree of control over the conditions and attributes of the connection.These aspectsare implemented using properties and event handlers of TADOConnection tofine-tune the connection.Specifying connection attributesUse the TADOConnection::ConnectOptions property to optionally force the connectionto be asynchronous.By default, ConnectionOptions is set to coConnectUnspecifiedWor k i ng wi t h ADO c omponent s 24-5Co n n e c t i n g t o ADO d a t a s t o r e swhich allows the server to decide the best type of connection.To explicitly make theconnection asynchronous, set ConnectOptions to coAsyncConnect.To set up a connection as asynchronous or to delegate the choice to the server, assignone of the TConnectOption constants to the connection component s ConnectOptionsproperty.Then activate the connection component by calling its Open method, settingthe Connected property to true, or by activating an associated command or datasetcomponent.The example routines below respectively enable and disableasynchronous connections in the specified connection component.void __fastcall TForm1::AsyncConnectButtonClick(TObject *Sender){ADOConnection1->Close();ADOConnection1->ConnectOptions = coAsyncConnect;ADOConnection1->Open();}void __fastcall TForm1::ServerChoiceConnectButtonClick(TObject *Sender){ADOConnection1->Close();ADOConnection1->ConnectOptions = coConnectUnspecified;ADOConnection1->Open();}Use the TADOConnection::Attributes property to control the connection component suse of retaining commits and retaining aborts.Attributes can contain one, both, orneither of the constants xaCommitRetaining and xaAbortRetaining.This makescontrolling retaining commits and retaining aborts mutually exclusive using thesame property.Check whether either retaining commits or retaining aborts is enabled using the inoperator with one of the constants.Enable one feature by adding the constant to theattributes property; disable one by subtracting the constant.The example routinesbelow respectively enable and disable retaining commits in the specified connectioncomponent.void __fastcall TForm1::RetainingCommitsOnButtonClick(TObject *Sender){ADOConnection1->Close()if (!ADOConnection1->Attributes.Contains(xaCommitRetaining))ADOConnection1->Attributes = TXactAttributes() Close()if (ADOConnection1->Attributes
[ Pobierz całość w formacie PDF ]