Using OLE DB to search

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello.  Ive been browsing these forums more often recently, but this is my first post.  Im trying to develop a console app that has a search functionality.  Im using Visual C++ 2008EE, with the latest Windows SDK.  The app is only meant for my laptop (Vista), so portability is not a concern.  After a lot of reading and some code writing, Ive come up with the following code.  I use an ISearchQueryHelper object to get an OLE DB connection string (which I read was the right way to do it).  The code compiles and runs fine.  I have a breakpoint at the end, before the clean-up statements.  When I check out the Autos tab and see the members of the datareader object (an OleDbDataReader), the Depth field is 0, which means that no results were found (I have a file called stopwordlist.txt in C:f).  What I suspected causing the issue was the connection string returned from ISearchQueryHelper.  Ive tried other connection strings Ive found online, but they crashed.  Now Im not sure if the connection string provided by the helper is the issue, or something else is wrong.  The connection string prints out as:<br/> <br/> provider=Search.CollatorDSO.1;EXTENDED PROPERTIES="Application=Windows"<br/> <br/> Since Im new at this, I cant help but feel that Im doing something terribly wrong, or going about doing it in a completely backwards way.<br/> <br/>
<pre lang="x-c# #include <SearchAPI.h>
#include <string>
#include <iostream>
#include <oledb.h>

#using <mscorlib.dll>
#using <System.data.dll>
#using <System.dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;


int main()
{
std::wstring userAQSQuery;

// code to get search parameters
// the query is in AQS form in
// userAQSQuery

HRESULT hr;

CoInitialize(NULL);

//code taken from MSDN on the ISearchQueryHelper page
ISearchManager* searchmanager;
ISearchCatalogManager* catalogmanager;
ISearchQueryHelper* queryhelper;

// Create ISearchManager instance
hr = CoCreateInstance(CLSID_CSearchManager, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&searchmanager));
// Call ISearchManager::GetCatalog for "SystemIndex" to access the catalog to the ISearchCatalogManager
hr = searchmanager->GetCatalog(L"SystemIndex", &catalogmanager);
// Call ISearchCatalogManager::GetQueryHelper to get the ISearchQueryHelper interface
hr = catalogmanager->GetQueryHelper(&queryhelper);

// Call get_ConnectionString to get the OLE DB connection string
LPWSTR connectionstring;

hr = queryhelper->get_ConnectionString(&connectionstring);

std::wcout << std::endl << connectionstring << std::endl;

LPWSTR generatedSQLQuery;

//generating the SQL query from the user is commented out
//hr = queryhelper->GenerateSQLFromUserQuery(userAQSQuery.c_str(), &generatedSQLQuery);

//this is used for testing/debugging
hr = queryhelper->GenerateSQLFromUserQuery(L"filename:stopwordlist path:C\f\", &generatedSQLQuery);

String^ str_connectionstring = gcnew String(connectionstring);
String^ str_generatedSQLQuery = gcnew String(generatedSQLQuery);

OleDbConnection^ connection = gcnew OleDbConnection();
OleDbCommand^ command = gcnew OleDbCommand();
command->CommandText = str_generatedSQLQuery;
connection->ConnectionString = str_connectionstring;
command->Connection = connection;
connection->Open();

OleDbDataReader^ datareader;
datareader = command->ExecuteReader();


//cleanup
datareader->Close();
CoTaskMemFree(connectionstring);
connection->Close();
}
[/code]
<br/>

View the full article
 
Back
Top