ch11lev1sec3

   

Database Connections

Connecting to the database is necessary so that the components can reach out through the driver to the actual database. Until a connection is made, no access to the database is possible.

The TADOConnection Class

This class takes the place of a combination of TSession and TDatabase and is generally used to establish the connection. As with TDatabase , it is not critical that this component be used ”each data component has the capability to form its own connection and can contain its own connection string.

The Provider

ADO connects to a database via a Provider , which is a named OLE object that implements an OLE DB (Object Linking and Embedding Database) interface. The provider is contacted by the ADO facility when ADO is given a connection string. Some of the connection string is used by the ADO facility; the rest is passed on to the provider.

The Connection String

The connection string combines the features of a BDE alias and the more complex information that stands behind the alias. It can also be identical to the ODBC connection string, in which case, the standard ODBC data provider will be used. That provider will access the specified installed ODBC driver to perform all operations.

Connection strings can be specific to the provider, but usually look something like the following (an example SQLServer connection string):

 Provider=SQLOLEDB.1;Persist Security Info=False; User ID=sa;Initial Catalog=mydatabase 

The connection string has the following components:

  • Provider is the name by which the provider COM object can be found in the registry. In practice, you cannot necessarily know this name in advance. You should instead prompt the user for the provider. The only method of determining available providers is to either use the rather complex OLE DB enumerator technology in its raw form (that is, unsupported by components, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/oledb/htm/oledbroot_enumerator_object.asp) or to use the dialog box provided for constructing connection strings (discussed in a following section).

  • User ID and Password can be supplied as is usually done in the Login Prompt property of BDE data components, except that under ODBC, the keywords are usually uppercase, whereas they are proper case in ADO.

  • Initial Catalog is the name of the database desired.

The Home of Transactions

The TADOConnection is the place to start, commit, or roll back transactions.

If you don't specify a separate connection object, the documentation implies you can use the Connection property of the TADOTable or TADOQuery object to access an internal connection. This is just as you would use the Database property of a BDE data component to access the default database object of a BDE TSession . Unfortunately, it does not work for ADO components because the Connection property remains NULL in an ADO object that has its own connection string, even once it is opened. Therefore, if you want transactions, you must create a separate TADOConnection object and use that as the connection for the TADOCustomDataSet descendant.

Accepting the Defaults

There are many properties and methods supplied by TADOConnection . In most cases, it is best to accept the defaults. Some instances where it is a good idea to change the defaults are discussed next under topics such as transactions and performance optimization.


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net