Delphi:TADOWrap
There is no dearth of components
, including the one native to the Delphi VCL, that offer simplified access to the capabilities of ADO. However, they are typically quite difficult to use because of the range of ADO functionality which they try to encapsulate. We offer a slimmed down alternative which is very easy to understand and to modify. TADOWrap is a TObject, not TComponent descendant so it is for the developer to create and manage instances of it at runtime as and when required. TADOWrap has six properties
-
Connected:Boolean- is true when there is an open ADO connection. FieldNames[Index:Integer]:String
- The names of the fields in the recordset returned by the last SQL query executed using the RunQuery method.FieldTypeByIndex[Index: Integer]:TVarType
- The variable types for the various field names reported above. Note that there isn't a perfect one-to-one mapping between ADO variable types and variant types. We have provided an adequate mapping for the more common types.FieldTypeByName[const Index: String]:TVarType
- Similar, but the fields are accessed via the fieldname.FieldByIndex[Index: Integer]:Variant
- This is the default property. Use it to get the values of the fields in each of the records returned by the last SQL query.FieldByName[const Index: String]:Variant
- Similar but uses fieldnames.
It should be noted that the methods which take a string or integer Index parameter raise an EADOWrap exception if an invalid/nonexistent index is supplied. TADOWrap has a number of methods for navigating through recordsets returned by the last SQL query which are for the most part self-explanatory. The methods that are less evident are listed below
constructor CreateEx(ATimeOut:Integer)
- This is the object constructor. ATimeOut, which must be in the range [1..60], is the number of seconds TADOWrap waits before reporting that aTADOWrap.ConnectorTADOWrap.RunCommandhas failed.procedure Connect(const ATo,AUserID,APwd: String)
- This method must be called before any other ADO operations can be performed. The ATo parameter specifies the ADO connectionstring to use and is obligatory. There are many excellent references to ADO connection strings on the Internet. This one is perhaps the most comprehensive of them all. The other two parameters, the UserID and password are optional - supply null strings if they are not relevant. If theADO.Connection.Openmethod fails an exception is triggered.procedure DisConnect
- Closes a previously opened ADO connection. TheTADOWrap.Destructorcalls DisConnect so there is not need to explicitly call it prior to destroying instances of the object.function RunCommand(const ACmd:String):Integer
- Use this method to run a SQL command such as DELETE,UPDATE or INSERT. The return value is the number of rows affected. Should the command fail to execute an exception will be raised.function RunQuery(const ASort,AQuery: String; AMax:Integer):Integer
- A more convenient way to run a SQL SELECT. Optionally, you can specify the field(s) to use for sorting the records that are returned via ASort. If the ADO engine supports the feature it is also possible to specify the maximum number of records to return. The return value is the actual number of records returned.
Run the sample application, ADOTest in the download, below, and then look at the source code.