Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

CreateRS.exe Uses the CreateRecordset Function in VC++


View products that this article applies to.

This article was previously published under Q190473

↑ Back to the top


Summary

The CreateRS.exe sample contains a TestRecordset.cpp file, which shows how to use the CreateRecordset() method of the Remote Data Services Datafactory or Data Control to create a new recordset that is not based on an existing table. This can be useful, for instance, when you want to input data from a non-standard source (such as instrumentation data from a serial port) and return a recordset with this data to the RDS Client.

Note If you are using MDAC 2.0 or later, it is recommended that you use the Append method of the Fields collection to create a new recordset instead. You can create a recordset and append fields to the Fields collection to define columns of the recordset.

To use the sample, create a new folder and copy CreateRS.exe into the folder. Run the .exe file, and it will extract the TestRecordset.cpp file. Run the Visual C++ environment and open TestRecordset.cpp. Pull down the Build menu, and tell it to build TestRecordset.exe. Visual C++ will ask if you want to create a new default workspace. Click Yes, and it should build. You shouldn't have to change anything, with the possible exception of the paths on the #import.

Note The type information has changed in Remote Data Services version 2.0. To use this sample, change references to DIBindMgrPtr to IBindMgrPtr before building if using Remote Data Services 2.0 or later.

↑ Back to the top


More information

The following file is available for download from the Microsoft Download Center:
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591� How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Using CreateRecordset

CreateRecordset requires a Variant of type VT_VARIANT|VT_ARRAY as a parameter, which describes the schema of the table. Each element of this SAFEARRAY holds another SAFEARRAY of variants describing each field. Each element of the field array must be of a specific vt VARIANT type or CreateRecordset returns an "Invalid Argument" or a "Wrong Parameter Type" error.

To begin with, each field in the new recordset requires a four-element safearray of variants. The elements represent the Field Name, the Field Type, the Field Length, and whether or not the Field is nullable, respectively.

The first element must have a type of VT_BSTR, the second element must have a type of VT_I2 and a value that corresponds to one of the ADO data types (you can find these in the header files, for example, adVarChar = 200). The third element has a type of VT_I2 and a value equal to the length of the field, and the fourth element has a type of VT_BOOL and a value of VARIANT_FALSE OR VARIANT_TRUE, depending on whether or not the "Not Null" property of the field should be set.

Once the safearrays for each field are built, another safearray must be built that will be the Schema of the recordset. The Schema Safearray (psaRecord in the sample) will be an array of variants and will have as many elements as the recordset will have fields. Set the type of each element in this array to VT_VARIANT | VT_ARRAY and load up each element with one of the field arrays above.

Finally, create a final variant that will hold the schema array (varRecord in the sample). Set the type to VT_ARRAY |VT_VARIANT and the value to the schema array. This is the parameter that will be passed to CreateRecordset.

Notes on the TestRecordset Sample

The sample does not use MFC, in the interests of simplicity and clarity. It does, however, use smartpointers and objects such as _variant_t and _bstr_t. These objects greatly simplify reference counting and scoping.

The sample also shows how to Add Records and then read through them. Several problems using RDS in Visual C++ and their workarounds are documented in the code comments, and should be read carefully.

In the version 2.5 and later of ADO/RDS there is an alternative to CreateRecordset. Simply create the recordset object, as in the sample. Then for each field in the recordset, create a Field object, fill in the required properties such as Name, Type, Length, etc, and Append the field to the Recordset. When all fields have been appended, simply Open the recordset and start adding data with AddNew, as in the sample.

↑ Back to the top


References

RDS documentation does not cover Visual C++. However, it is useful for a description of what the functions/properties of the objects do. Also, the .tli and .tlh files that the #import directive creates will provide the specific Visual C++ syntax for a method call.

↑ Back to the top


Keywords: KB190473, kbsample, kbhowto, kbfile, kbdatabase, kbdownload

↑ Back to the top

Article Info
Article ID : 190473
Revision : 8
Created on : 3/2/2005
Published on : 3/2/2005
Exists online : False
Views : 521