Hi, I'm trying to use the following piece of code to obtain the column names from the table in SQL Server, but somehow I'm getting the wrong number of columns in ul_numColumns (see code), and only the first column name in pstr_stringBuffer. Presumably I should be getting a whole string with all the column names there, but I don't. Any suggestions for what I may be doing wrong?..
Thanks in advance
int GetColumnNames(void)
{
try
{
pCommand.CreateInstance(__uuidof (Command));
pCommand->ActiveConnection = pConn;
pCommand->CommandText = "SELECT * FROM t25_pallet_status"; // SQL Syntax...
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open( (IDispatch *) pCommand, vtMissing, adOpenStatic,
adLockReadOnly, adCmdUnknown);
// Get ADORecordsetConstruction interface from the the ADO Recordset
ADORecordsetConstruction *p_adoRecordsetConstruct;
pRecordset->QueryInterface(__uuidof(ADORecordsetConstruction),
(void **)&p_adoRecordsetConstruct);
// From it, we can get the OLEDB <code>IRowset
IRowset *p_rowset;
p_adoRecordsetConstruct->get_Rowset((IUnknown **)&p_rowset);
p_adoRecordsetConstruct->Release(); // don't need it anymore
// The IColumnsInfo that contains ordinals
CComPtr<IColumnsInfo> spColumns;
// Get the the IColumnsInfo from IRowset interface
p_rowset->QueryInterface(&spColumns);
// At this point, we may now release p_rowset
p_rowset->Release();
// IColumnsInfo will give us the DBCOLUMNINFO structure
ULONG ul_numColumns;
DBCOLUMNINFO *p_columnInfo = NULL;
OLECHAR *pstr_stringBuffer = NULL;
// Now get the DBCOLUMNINFO data
spColumns->GetColumnInfo(&ul_numColumns, &p_columnInfo, &pstr_stringBuffer);
// Clean up
CoTaskMemFree(p_columnInfo);
CoTaskMemFree(pstr_stringBuffer);
}
catch(_com_error & ce)
{
PrintComError(ce);
return 0;
}
}
You can do the following to get all the column names
for(ULONG j=0; j<ul_numColumns; j++)
printf("%S\t", p_columnInfo[j].pwszName);
Hope this helps
|||Thanks a lot Raj,
I kind of sussed it out for my self a short while after I posted the message. I didn't really understand how GetColumnInfo() worked, and I was expecting pstr_stringBuffer to return a string with ALL of the column names (separated by a common delimitter or something). It became apparent to me that I had to loop through and read from a different variable (pwszName) in order to get those names.
I have to say there's very little on examples out there to use functions like this one. Can anyone recomend me a good website where I can get sample code from in C or C++ for the Visual Studio 2003 environment?
Cheers
No comments:
Post a Comment