Microsoft KB Archive/198486

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

Article ID: 198486

Article Last Modified on 1/11/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition

-



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 using namespace std ;

#include   #include  // atlimpl is obsolete in versions later than 6.0

CComModule _Module;

#define DBINITCONSTANTS #include   #include    #include 

//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 > product ;

// ===================================================     // Setting OLE DB Initialization Properties // ===================================================     CDBPropSet propset(DBPROPSET_DBINIT); propset.AddProperty(DBPROP_INIT_DATASOURCE, L""); propset.AddProperty(DBPROP_INIT_CATALOG, L"NorthWind"); propset.AddProperty(DBPROP_AUTH_USERID, L""); propset.AddProperty(DBPROP_AUTH_PASSWORD, L""); 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 ; }  }

