|
Introduction dbGo components TADOConnection component TADOTable component TADODataSet component Sending changes to database - updatable rowsets technology Client and server cursors What provider to choose? TADOQuery component SQL-queries with parameters TADOStoredProc component TADOCommand component Transactions Useful links Download atricle source code: Firebird,Interbase è Delphi Download IBProvider Professional Edition |
||
Introduction. Access to Interbase and Firebird from DelphiThere exist several means of working with Interbase and Firebird in Delphi. I know minimum 3 ways of accessing data via IBProvider:
dbGo componentsdbGo are Borland VCL-components which allow regular way of working with ADO library from Delphi and C++ Builder for this development tool. Prior to Delphi 6 the components were called ADOExpress. The appearance of dbGo components panel in Delphi (RAD Studio 2007):
dbGo components include:
Creating connection to the Firebird/Interbase databaseTADOConnection component connects to the data storage. TADOConnection resembles TSQLConnection component. The difference between them is that when working with TSQLConnection connection parameters are set via ConnectionName, DriverName and Params properties. In TADOConnection all connection parameters are set via ConnectionString property. Moreover, it is possible to specify file name with connection parameters as a connection string in TADOConnection. Special file type Microsoft Data Link exists to store connection parameters in Windows - it is a file with udl extension. Universal connection editor associates with this extension. IBProvider supports its own tabs that provide convenient interface for connection parameters forming. To use udl file in your application follow the next steps:
![]()
![]()
Now add TADOConnection to the form. Set the property: Login Promt=false and specify previously created udl connection file as ConnectionString: ![]() Detailed information on IBProvider initialization properties page. Data display via TADOTableTADOTable component allows loading data from single table.
As a result DBGrid will display data from the table chosen in TADOTable object: ![]() TADODataSet componentTADODataSet component is the add-in to ADODB.Recordset object. Prior to start working with TADODataSet set the property Connection = ADOConnection1. An alternative variant is to set connection string in ConnectionString property. But I strongly recommend to use separate TADOConnection object for connection storage. Enter the text of data loading command in CommandText property. The command text may be:
A command in CommandText shall return the recordset. For example, you can not use INSERT, DELETE or UPDATE and stored procedures that do not return the recordset in the text. Let’s pass to the practical example demonstrating Delphi abilities when working with Firebird/Interbase:
The finished example is shown on the figure: ![]() The following is the sequence of actions to reproduce the example by your own:
TDBCtrlGrid - is a container component. It can display both one and several records depending on RowCount property value. It contains components for data editing. TDBLabel - is a readonly field. We will use this component to display FULL_NAME column. To edit FIRST_NAME, LAST_NAME fields we add 2 TDBText components to the form and set DataField property for them. JOB_COUNTRY field has FOREIGN KEY on COUNTRY table. To edit this field we use drop-down list TDBComboBox containing country codes. After setting DataField = JOB_COUNTRY you shall fill in the list with data from the table COUNTRY. In addition to those described in the example Data Controls tab contains components for image editing (TDBImage), multi-page text (TDBMemo), lists displaying, etc. Sending changes to database - updatable rowsets technologyIBProvider supports a wonderful OLE DB option - updatable rowsets technology. In the above example this technology allowed not to specify the logic of the changes sending back to the database in the code. All the changes were send automatically via updatable rowsets. Currently only IBProvider v2 supports updatable rowsets technology. Please, take it into account when choosing a provider for connection. Client and server cursorsIBProvider uses cursors to navigate through records. Cursors can be server or client ones. Client cursor is stored on the client’s side. After running a query the whole resulting set is loaded to ram-memory or driver swap-file. It can cause delays at large volume data transfers, especially via the LAN. In dbGo client cursor is used by default. It provides an advantage of quick operation when working with small data volumes. All the three providers from IBPRovider Professional Edition can work with client cursors. Server cursor is used when working with large data sets that are disadvantageous to be sent to clients in full. So the cursor is returned to the client immediately after query running and data is read on as-needed basis. In addition, IBProvider implementation allows to read BLOB-fields on as-applied basis instead of receiving the whole record. It also raises production efficiency when one entry contains several BLOB-columns with large data. In the current implementation server cursor can be used only with IBProvider v1, as it is the only one supporting 4-byte bookmarks used in dbGo for working in server cursor mode. The later IBProvider versions already use 8-byte bookmarks. Cursor type is specified in CursorLocation property. It can have values clUseServer or clUseClient. What provider to choose?IBProvider Professional includes three OLE DB providers with different opportunities for developers. IBProvider v1 allows working with dbGo components in server cursor mode. This is because of the limited bookmark size in dbGo = 4 bytes. We have already planned to make IBProvider v3 compatible with dbGo server cursors in Delphi and C++ Builder. As soon as it is implemented, IBProvider v1 will become history. To use the provider specify Provider=LCPI.IBProvider.1 in the connection string. IBProvider v2 supports updatable rowsets technology. They allow to transfer changes back to the database without explicit entering of insert/delete/update command texts. The provider generates SQL-commands individually based on select-expressions. This technology usage example can be seen above. To use the provider specify Provider=LCPI.IBProvider.2 in the connection string. Our near-term plans include implementation of updatable rowsets support in IBProvider v3. IBProvider v3 is the most up-to-date and productive provider. It has the unique set of technologies, supports all code pages, special options of the latest versions of Firebird and Interbase servers, 64-bit operating systems and many other useful functions. If you do not plan to use updatable rowsets or server cursor choose IBProvider v3 without doubts. To do this specify Provider=LCPI.IBProvider.3 or Provider=LCPI.IBProvider in the connection string. If you need to use the opportunities of all three drivers in one application, I recommend you, as a temperate solution, to use three identical connections to the database with different drivers. For instance, to do all the basic operations via IBProvider v3, updatable rowsets via IBProvider v2, to cache data via sever cursors with IBProvider v1. After we have updatable sets and dbGo server cursors support available in IBProvider v3 you will be able to leave only one connection. TADOQuery componentTADOQuery component is aimed at execution of SQL-commands. It can be considered as an analog of TSQLQuery component from dbExpress. Connection to the database is set via Connection or ConnectionString properties. Query text is written to SQL property. If a query returns the dataset one should use Open() method or Active=true property. If a query does not return the dataset, it shall be run using ExecSQL method. ExecSQL returns the number of records processed during a query running. The same value is contained in RowsAffected property. SQL-queries with parametersQueries can have parameters. Using command parameters one may set conditions and pass data to the database server. There are two types of parameters: named and positional. By default all named parameters in IBProvider are set via a semicolon (:), positional ones via a question mark (?). To demonstrate the work of TADOQuery component we will write an example in which three variants of its application can be seen:
The finished example is shown on the figure. TADOQuery component is used for rowset selection and insert/delete operations performing: ![]() Components on the form are connected in the following way: TADOQuery requests data from the database specified in TADOConnection and transfers it to TDataSource intermediary component. TDBGrid is able to display data loaded into TDataSource. The following interaction scheme appears: ADOConnection->ADOQuery->DataSource (Data Access tab)->DBGrid Let’s study the first variant of TADOQuery usage - running of queries that do not return resulting rowset:
In the above example command parameters are set by two different ways: 1. By one expression at a time:
2. And each parameter separately:
To select data via TADOQuery SQL expression is used which contains SELECT command and Open() method: TADOStoredProcTADOStoredProc component allows execution of Firebird and Interbase stored procedures. TADOStoredProc work principle is the same as that of TADOQuery:
The only difference is ProcedureName property in which the name of stored procedure is defined. SQL property is absent in TADOStoredProc component. Stored procedure can return the result in two ways:
To obtain resulting rowset SELECT operator is used:
To execute the procedure that does not return anything or returns OUT-parameters the exec instruction shall be used:
TADOCommand componentTADOCommand is the command send to server to read or change data. Actually, the component unites the abilities of 3 components studied: TADOTable, TADOQuery, TADOStoredProc. TADOCommand behavior changes depending on CommandType property. It can take the following values:
Working with transactionsTransactions controlling mechanisms in IBProvider are the same for all development tools. I described the transactions earlier in the first part of Interbase and Firebird Developer's manual for Visual Studio .Net . I recommend you to pay attention to the following chapters of it:
Useful links
| ||