SAMPLE: CreateRS.exe Uses the CreateRecordset Function in VC++

ID: Q190473

The information in this article applies to:

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.

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.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ CreateRS.exe (size: 25474 bytes) 

Release Date: JUN-24-1998

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE : How to Obtain Microsoft Support Files from Online Services

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.

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.

Additional query words: kbVC kbVC600 kbRDS

Keywords          : kbsample
Version           : WINDOWS:1.5
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: July 30, 1998