How to use ATL OLE DB Consumer to perform a full-text query with Visual C++ (198486)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft SQL Server 7.0

This article was previously published under Q198486
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

This sample code below is an OLE DB Consumer Application that demonstrates how to perform a full-text query with the Visual C++ 6.0 OLE DB Consumer Templates to retrieve data from the SQL Server NorthWind database using SQLOLEDB Provider.

Full-text indexes can be administered using either the SQL Server Enterprise Manager or stored procedures. Before you run this sample, run the FullText Indexing Wizard (in SQL Server's Enterprise Manager) on the NorthWind.Products table, and index the ProductName column. Populate the index by right-clicking its name under the Full-text catalogs folder in the NorthWind database. For more information, see SQL Books Online.

Briefly, create a .cpp file using the code given below in Visual C++ 6.0 and compile/build from Build menu (it will prompt for creating a new workspace--this should build and run without adding new code). Change the initialization properties to connect to your SQL 7.0 server.

MORE INFORMATION

Microsoft SQL Server version 7.0 provides the ability to perform full-text queries on character data stored in SQL Server tables. For example:
   SELECT ProductID, ProductName FROM Products
   WHERE CONTAINS(ProductName,'spread NEAR () Boysenberry')
				
The following sample code is an OLE DB Consumer Application that uses OLE DB Consumer Template classes.

Sample code

   #include <iostream>
   using namespace std ;

   #include <atlbase.h>
   #include <atlimpl.cpp> // atlimpl is obsolete in versions later than 6.0

   CComModule _Module;

   #define DBINITCONSTANTS
   #include <atlcom.h>
   #include <atldbcli.h>
   #include <msdaguid.h>

   //Accessor Class.
   class CProductsAccessor
   {
   public:
      LONG m_ProductID;
      TCHAR m_ProductName[41];

   //output binding map.
   BEGIN_COLUMN_MAP(CProductsAccessor)
      COLUMN_ENTRY(1, m_ProductID)
      COLUMN_ENTRY(2, m_ProductName)
   END_COLUMN_MAP()
   };

   void main()
   {
      HRESULT hr ;

      //Initialize COM.
      hr = CoInitialize(NULL);

      CDataSource connection ;

      CSession session ;

      CCommand<CAccessor<CProductsAccessor> > product ;

      // ===================================================
      // Setting OLE DB Initialization Properties
      // ===================================================
      CDBPropSet propset(DBPROPSET_DBINIT);
      propset.AddProperty(DBPROP_INIT_DATASOURCE, L"<SQL Server Name>");
      propset.AddProperty(DBPROP_INIT_CATALOG, L"NorthWind");
      propset.AddProperty(DBPROP_AUTH_USERID, L"<User ID>");
      propset.AddProperty(DBPROP_AUTH_PASSWORD, L"<Password>");
      hr = connection.Open("SQLOLEDB", &propset);

      // Open a session.
      hr = session.Open(connection) ;

      hr = product.Open(session, "SELECT ProductID, ProductName FROM
         Products WHERE CONTAINS(ProductName,'spread NEAR Boysenberry')");

      //Get data from the rowset.
      while(product.MoveNext() == S_OK)
      {
         cout << product.m_ProductID << " ";
         cout << product.m_ProductName << endl ;
      }
   }

REFERENCES

For more details on SQL 7.0 full-text search, please refer to SQL 7.0 Books Online.

To learn more about the Accessor used in the code above, run the ATL Consumer Wizard on the SQL Server 7 NorthWind database, Products Table.

For more information about the requirements to add OLE DB template support to Visual C++ projects, click the following article number to view the article in the Microsoft Knowledge Base:

190959 PRB: OLE DB Consumer Template Wizard requires ATL project


Modification Type:MajorLast Reviewed:1/11/2006
Keywords:kbDatabase kbDTL kbhowto KB198486 kbAudDeveloper