///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;
//Open an IADORecordBinding interface pointer which we'll use
//for Binding Recordset to a class
TESTHR(pRstPubInfo->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs));
//Bind the Recordset to a C++ Class here
TESTHR(picRs->BindToRecordset(&pubrs));
//Display the available logos here
strMessage = "Available logos are: " + (_bstr_t)"\n\n";
printf(strMessage);
int Counter = 0;
while(!(pRstPubInfo->EndOfFile))
{
printf("\n%s",pubrs.m_sz_pubid);
printf("\n%s",strtok(pubrs.m_sz_prinfo,","));
//Display 5 records at a time and wait for user to continue.
if (++Counter >= 5)
{
Counter = 0;
printf("\nPress any key to continue...");
getch();
}
pRstPubInfo->MoveNext();
}
//Prompt For a Logo to Copy
printf("\nEnter the ID of a logo to copy: ");
scanf("%s",pubId);
strPubID = pubId;
//Copy the logo to a variable in chunks
pRstPubInfo->Filter = "pub_id = '" + strPubID + "'";
lngLogoSize = pRstPubInfo->Fields->Item["logo"]->ActualSize;
//Create a safe array to store the array of BYTES
rgsabound[0].cElements = lngLogoSize;
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
//Copy the data only up to the Actual Size of Field.
for(long index=0;index<=(ChunkSize-1);index++)
{
hr= SafeArrayGetElement(varChunk.parray,&index,&chData);
if(SUCCEEDED(hr))
{
//Take BYTE by BYTE and advance Memory Location
TESTHR(SafeArrayPutElement(psa,&index1,&chData));
index1++;
}
else
break;
}
lngOffSet = lngOffSet + ChunkSize;
}
lngOffSet = 0;
printf("Enter a new Pub Id: ");
scanf("%s",pubrs.m_sz_pubid);
strPubID = pubrs.m_sz_pubid;
// Temporarily add new publisher to table to avoid error due
// to foreign key constraint.
pConnection->Execute("INSERT publishers(pub_id) VALUES('" +
strPubID + "')",NULL,adCmdText);
//Show the newly added record.
printf("New Record : %s\nDescription : %s\nLogo Size : %s\n",
pubrs.m_sz_pubid,
pubrs.m_sz_prinfo,(LPCSTR)(_bstr_t)pRstPubInfo->Fields->
Item["logo"]->ActualSize);
//Delete new records because this is demonstration.
pConnection->Execute("DELETE FROM PUB_INFO WHERE pub_id = '"
+ strPubID +"'",NULL,adCmdText);
pConnection->Execute("DELETE FROM publishers WHERE pub_id = '"
+ strPubID +"'",NULL,adCmdText);
pRstPubInfo->Close();
pConnection->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
MS SQL Server OLE DB connection using an IP address
Provider=SQLOLEDB; Data Source=xx.xx.xx.xx,1433; Network Library=DBMSSOCN; Initial Catalog=dbname;User ID=sa;Password=pass;
see AppendChunk and GetChunk Methods Example (VC++) in MSDN