Click here to Skip to main content
15,891,372 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Problems again- Newbie Pin
Nibu babu thomas31-May-06 0:35
Nibu babu thomas31-May-06 0:35 
GeneralRe: Problems again- Newbie Pin
antonaras31-May-06 1:17
antonaras31-May-06 1:17 
GeneralRe: Problems again- Newbie Pin
Nibu babu thomas31-May-06 1:29
Nibu babu thomas31-May-06 1:29 
GeneralRe: Problems again- Newbie Pin
antonaras31-May-06 1:20
antonaras31-May-06 1:20 
GeneralRe: Problems again- Newbie Pin
ThatsAlok31-May-06 2:31
ThatsAlok31-May-06 2:31 
AnswerRe: Problems again- Newbie Pin
Steve S31-May-06 0:45
Steve S31-May-06 0:45 
GeneralRe: Problems again- Newbie Pin
antonaras31-May-06 1:21
antonaras31-May-06 1:21 
AnswerRe: Problems again- Newbie Pin
Steve S31-May-06 4:15
Steve S31-May-06 4:15 
This isn't pretty, nor is it optimal, but it covers the basics; You'll want to reindent the code, Smile | :) and I do things a little differently, by using raw interfaces when importing.
Where there is a call to doQuery, you can put so UI in there to ask for & validate a query field. This is a bit rough in places, normally I use OLE DB rather than ADO, but it works. You'll need to change the name of the data source (obviously).
The trick is to create the parameter and associate it with the command object. You can then use Execute to get a recordset, and having done that, you can ask the recordset to requery without needing the command object (once you've changed the parameter value. Add a different call to doQuery with a different string below the first one, and you'll see what I mean.

The advantage over straight SQL text building is that in many cases, the SQL needs to be parsed only once, which is handy for complex queries, and in addition, it protects you from some nasty so-and-so from performing a SQL injection attack.

(Yes, it was asking a bit much, but I had a spare 10 minutes at lunchtime[local time])

<br />
#include <windows.h><br />
#include <tchar.h><br />
#include <ole2.h><br />
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \<br />
    no_namespace rename("EOF", "EndOfFile") raw_interfaces_only<br />
#include <stdio.h><br />
#include <conio.h><br />
<br />
<br />
void printRows(_Recordset* pRstTemp)<br />
{<br />
	VARIANT_BOOL bEOF;<br />
    // Ensure at top of recordset.<br />
    pRstTemp->MoveFirst();<br />
<br />
    // If EOF is true, then no data and skip print loop.<br />
	pRstTemp->get_EndOfFile(&bEOF);<br />
	if (bEOF)<br />
    {<br />
        _tprintf(_T("\tRecordset empty\n"));<br />
    }<br />
    else<br />
    {<br />
        // Define temporary strings for output conversions.<br />
        // Initialize to first record's values.<br />
        _bstr_t bstrTitle;<br />
        _bstr_t bstrType;<br />
<br />
        // Enumerate Recordset and print from each.<br />
        while(!bEOF)<br />
        {<br />
			// Convert variant string to convertable string type.<br />
			FieldsPtr spFields;<br />
			FieldPtr spField;<br />
			long nFields;<br />
			pRstTemp->get_Fields(&spFields);<br />
			spFields->get_Count(&nFields);<br />
			for(long f = 0; f < nFields; f++)<br />
			{<br />
				BSTR t = NULL;<br />
<br />
				spFields->get_Item(_variant_t(f), &spField);<br />
				_variant_t v;<br />
				spField->get_Value(&v);<br />
				spField->get_Name(&t);<br />
				if (v.vt != VT_NULL)<br />
				{<br />
					v.ChangeType(VT_BSTR);<br />
#ifdef	_UNICODE<br />
					_tprintf(_T("%20.20s: %s\n"), (LPCWSTR)t, (LPCWSTR)v.bstrVal);<br />
#else<br />
					_tprintf(_T("%20.20S: %S\n"), (LPCWSTR)t, (LPCWSTR)v.bstrVal);<br />
#endif<br />
				}<br />
				else<br />
				{<br />
#ifdef	_UNICODE<br />
					_tprintf(_T("%20.20s: (null)\n"), (LPCWSTR)t);<br />
#else<br />
					_tprintf(_T("%20.20S: (null)\n"), (LPCWSTR)t);<br />
#endif<br />
				}<br />
				SysFreeString(t);<br />
				spField = NULL;<br />
			}<br />
			_tprintf(_T("\n"));<br />
			pRstTemp->MoveNext();<br />
			pRstTemp->get_EndOfFile(&bEOF);<br />
        }<br />
    }<br />
}<br />
<br />
void doQuery(_CommandPtr& pCmd, _RecordsetPtr& pRS, _ParameterPtr& pParam, LPCTSTR custid)<br />
{<br />
	HRESULT hr;<br />
	pParam->put_Value(_variant_t(custid));<br />
	if (pRS==NULL)<br />
	{<br />
		hr = pCmd->Execute(NULL,NULL,0,&pRS);<br />
	}<br />
	else<br />
	{<br />
		hr = pRS->Requery(0);<br />
	}<br />
	if (SUCCEEDED(hr))<br />
	{<br />
		printRows(pRS);<br />
	}<br />
}<br />
<br />
void doSample()<br />
{<br />
	HRESULT hr;<br />
	_ConnectionPtr spConn;<br />
	_CommandPtr spCommand;<br />
	_RecordsetPtr spRS;<br />
	_ParameterPtr spParam;<br />
	ParametersPtr spParamSet;<br />
	_variant_t custid;<br />
<br />
	hr = spConn.CreateInstance(__uuidof(Connection));<br />
	if (SUCCEEDED(hr))<br />
	{<br />
		_bstr_t empty(_T(""));<br />
		_bstr_t sample(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=nwind.mdb"));<br />
		hr = spConn->Open( sample, empty, empty, adModeUnknown);<br />
		if (SUCCEEDED(hr))<br />
		{<br />
			hr = spCommand.CreateInstance(__uuidof(Command));<br />
			if (SUCCEEDED(hr))<br />
			{<br />
				spCommand->put_CommandText(_bstr_t("SELECT * FROM customers WHERE CustomerID = ?"));<br />
				spCommand->put_Prepared(VARIANT_TRUE);<br />
				spCommand->putref_ActiveConnection(spConn);<br />
				hr = spCommand->Execute(NULL,NULL,adCmdText, &spRS);<br />
				hr = spCommand->get_Parameters(&spParamSet);<br />
				spParamSet->Refresh();<br />
				hr = spCommand->CreateParameter(NULL,adBSTR,adParamInput,NULL,custid,&spParam);<br />
				spParamSet->Append(spParam);<br />
<br />
				// Here you can call doQuery any number of times...<br />
				doQuery(spCommand, spRS, spParam, _T("ALFKI"));<br />
			}<br />
			else<br />
			{<br />
				_tprintf(_T("Unable to create command [HR=0x%8lX]\n"),hr);<br />
			}<br />
			spConn->Close();<br />
		}<br />
		else<br />
		{<br />
			_tprintf(_T("Unable to open connection [HR=0x%8lX]\n"),hr);<br />
		}<br />
	}<br />
	else<br />
	{<br />
		_tprintf(_T("Unable to create connection object [HR=0x%8lX]\n"), hr);<br />
	}<br />
}<br />
<br />
<br />
int main(int argc, char**argv)<br />
{<br />
	if (SUCCEEDED(CoInitialize(NULL)))<br />
	{<br />
		doSample();<br />
		CoUninitialize();<br />
	}<br />
	return 0;<br />
}



Steve S
Developer for hire
AnswerRe: Problems again- Newbie Pin
BadKarma31-May-06 1:37
BadKarma31-May-06 1:37 
GeneralRe: Problems again- Newbie Pin
antonaras31-May-06 1:43
antonaras31-May-06 1:43 
AnswerRe: Problems again- Newbie Pin
Hamid_RT31-May-06 1:56
Hamid_RT31-May-06 1:56 
QuestionFindNextPrinterChangeNotification - Invalid Handle Pin
P Gibson31-May-06 0:20
P Gibson31-May-06 0:20 
AnswerRe: FindNextPrinterChangeNotification - Invalid Handle Pin
NiceNaidu31-May-06 0:57
NiceNaidu31-May-06 0:57 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
NiceNaidu31-May-06 1:05
NiceNaidu31-May-06 1:05 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
P Gibson31-May-06 1:19
P Gibson31-May-06 1:19 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
NiceNaidu31-May-06 1:36
NiceNaidu31-May-06 1:36 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
P Gibson31-May-06 2:35
P Gibson31-May-06 2:35 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
Member 45238505-Apr-09 16:05
Member 45238505-Apr-09 16:05 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
pgibson0077006-Apr-09 0:55
pgibson0077006-Apr-09 0:55 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
Member 45238507-Apr-09 2:17
Member 45238507-Apr-09 2:17 
GeneralRe: FindNextPrinterChangeNotification - Invalid Handle Pin
honohsu14-Apr-09 15:24
honohsu14-Apr-09 15:24 
QuestionRe: FindNextPrinterChangeNotification - Invalid Handle Pin
David Crow31-May-06 5:25
David Crow31-May-06 5:25 
AnswerRe: FindNextPrinterChangeNotification - Invalid Handle Pin
pgibson00770031-May-06 9:08
pgibson00770031-May-06 9:08 
QuestionRe: FindNextPrinterChangeNotification - Invalid Handle Pin
David Crow31-May-06 9:18
David Crow31-May-06 9:18 
AnswerRe: FindNextPrinterChangeNotification - Invalid Handle Pin
pgibson00770031-May-06 10:54
pgibson00770031-May-06 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.