An rdoQuery object is a query definition that can include zero or more parameters.
Remarks
The rdoQuery object is used to manage SQL queries requiring the use of input, output or input/output parameters. Basically, an rdoQuery functions as a compiled SQL statement. When working with stored procedures or queries that require use of arguments that change from execution to execution, you can create an rdoQuery object to manage the query parameters. If your stored procedure returns output parameters or a return value, or you wish to use rdoParameter objects to handle the parameters, you must use an rdoQuery object to manage it. For example, if you submit a query that includes information provided by the user such as a date range or part number, RDO can substitute these values automatically into the SQL statement when the query is executed.
Note The rdoQuery object replaces the outdated rdoPreparedStatement object. The rdoQuery object remains similar to the rdoPreparedStatement in its interface, but adds the ability to be persisted into a Visual Basic project, allowing you to create and manipulate it at design time. Additionally, the rdoQuery objects can be prepared or not, allowing the you to choose the most appropriate use of the query.
Creating rdoQuery Objects
To create an rdoQuery object, use the CreateQuery method which associates the rdoQuery with a specific rdoConnection object and adds it to the rdoQueries collection. Once created, you must fill in required parameters using the rdoParameters collection, and then use the OpenResultset method to create resultsets from the query, or the Execute method to simply run the query if it does not return rows.
You can also use the User Connection Designer (CQD) to create rdoQuery objects in your project. The CQD takes your SQL query and permits you to specify the data types for each parameter. It then inserts appropriate code in your application to expose these procedures very much like methods off of the rdoQuery object.
Note Due to the extra overhead involved in creating and managing rdoQuery objects and the rdoParameters collection, you should not use parameter queries for SQL statements that do not change from execution to execution — especially those that are executed only once or infrequently.
Stand Alone rdoQuery Objects
You can declare a stand-alone rdoQuery object using the Dim statement as follows:
Dim MyQuery as New rdoQuery
Stand-alone rdoQuery objects are not assigned to a specific rdoConnection object, so you must set the ActiveConnection property before attempting to execute the query, or to use the OpenResultset object against it. The CursorType and ErrorThreshold properties are set from default values established by the rdoEngine default settings. In addition, new rdoQuery objects are not automatically appended to the rdoQueries collection until you use the Add method.
For example, the code shown below creates an rdoQuery object, associates it with a connection, and executes it. Next, the rdoQuery object is associated with a different connection and executed again. The query object becomes more of an encapsulation of any kind of query, and thus can be executed against any kind of connection, provided the SQL statement would be appropriate for the connection.
Dim MyQuery As rdoQuery '
MyQuery.SQL = "Update customers " _
& " Set LastTouched = GetDate()"
MyQuery.Prepared = False 'don't prepare it,
'just SQLExecDirect
'assume that cnSomeConnection
'is an rdoConnection or stand-alone object
MyQuery.ActiveConnection = cnSomeConnection
MyQuery.Execute
MyQuery.ActiveConnection = cnOtherConnection
'the cnOtherConnection is over a WAN, so I can increase
'my query timeout to compensate
MYQuery.QueryTimeout = 120
MyQuery.Execute
Choosing the right SQL Syntax
When coding the SQL property of an rdoQuery object, you can choose between one of three syntax styles to code your parameter query:
sSQL = "Select Name, Age From Animals " _
& " Where Weight > " & WeightWanted.Text _
& " and Type = ' & TypeWanted.Text & "'"
sSQL = "Select Au_LName from Authors" _
& " Where Au_Fname = ?"
– Or –
sSQL = "Execute MyStoredProc 'Arg1', 450, '" _
& Text1
– Or –
sSQL = "Execute MyStoredProc ?, ?, ?"
sSQL = "{call ParameterTest (?,?,?) }"
– Or –
sSQL = "{? = call ParameterTest (?,?,?) }"
– Or –
sSQL = "{? = call CountAnimals (?, ?, 14, 'Pig')}
The rdoQuery object is managed by setting the following properties and methods.
rdoQuery Object Events
The following events are fired as the rdoQuery object is manipulated. These can be used to micro-manage queries associated with the rdoQuery or coordinate other processes in your application.
Event Name | Description |
QueryComplete | Fired when a query has completed. |
QueryTimeout | Fired when the QueryTimeout period has elapsed and the query has not begun to return rows. |
WillExecute | Fired before the query is executed permitting last-minute changes to the SQL, or to prevent the query from executing. |
Closing the rdoQuery Object
Use the Close method to close an rdoQuery object, set its ActiveConnection property to Nothing, and remove it from the rdoQueries collection. However, you can still re-associate the rdoQuery object with another rdoConnection object by setting its ActiveConnection property to another rdoConnection object. Using the Execute method or OpenResultset method against an rdoQuery object that has its ActiveConnection property set to Nothing or an invalid rdoConnection causes a trappable error.
Addressing rdoQuery Objects
rdoQuery objects are the preferred way to submit parameter queries to the external server. For example, you can create a parameterized Transact SQL query (as used on Microsoft SQL Server) and store it in an rdoQuery object.
You refer to an rdoQuery object by its Name property setting using the following syntax. Since the rdoQuery object's default collection is the rdoParameters collection, all unqualified references to the rdoQuery object refer to the rdoParameters collection. In these examples, assume we have created an rdoQuery object named rdoQo
using the syntax Dim rdoQo as rdoQueries
. The first two examples refer to the rdoQuery object named "MyQuery".
rdoQo("MyQuery")
– Or –
rdoQo!MyQuery
You can also refer to rdoQuery objects (and the rdoPreparedStatements collection) by their position in the rdoQueries collection using this syntax (where n is the nth member of the zero-based rdoQueries collection):
rdoQo(n)