northwindoledb相关问题
HRESULThr= NOERROR;DBBINDING*prgBinding= NULL;HROWrghRows[1];HROW*prghRows= rghRows;DBIDTableID;DBIDIndexID;DBPROPSETrowsetpropset[1];DBPROProwsetprop[1];ULONGcRowsObtained= 0;DBOBJECTdbObject;DBCOLUMNINFO*pDBColumnInfo= NULL;BYTE*pData= NULL;WCHAR*pStringsBuffer= NULL;
DWORDdwBindingSize= 0;
DWORDdwIndex= 0;
DWORDdwOffset= 0;
DWORDdwOrdinal= 0;
ULONGulNumCols;
IOpenRowset*pIOpenRowset= NULL;// Provider Interface Pointer
IRowset*pIRowset= NULL;// Provider Interface Pointer
IRowsetIndex*pIRowsetIndex= NULL;// Provider Interface Pointer
IAccessor*pIAccessor= NULL;// Provider Interface Pointer
ILockBytes*pILockBytes= NULL;// Provider Interface Pointer
IColumnsInfo*pIColumnsInfo= NULL;// Provider Interface Pointer
HACCESSORhAccessor= DB_NULL_HACCESSOR;// Accessor handle
WCHAR*pwszEmployees[]={// Employee info Column names
L"Name",
L"No",
L"X",
L"Y",
L"Z",
L"P"
};
/*CREATE TABLE Step(Name NVARCHAR(20),No NVARCHAR(20),X NVARCHAR(20),Y NVARCHAR(20),Z NVARCHAR(20),P NVARCHAR(20),与数据库对应的columnm名字*/
VariantInit(&rowsetprop[0].vValue);
// Validate IDBCreateSession interface
//
if (NULL == m_pIDBCreateSession)
{
hr = E_POINTER;
goto Exit;
}
// Create a session object
//
hr = m_pIDBCreateSession->CreateSession(NULL, IID_IOpenRowset, (IUnknown**) &pIOpenRowset);
if(FAILED(hr))
{
goto Exit;
}
// Set up information necessary to open a table
// using an index and have the ability to seek.
//
TableID.eKind= DBKIND_NAME;
TableID.uName.pwszName= (WCHAR*)L"Step";
IndexID.eKind= DBKIND_NAME;
IndexID.uName.pwszName= L"I_Step";
// Request ability to use IRowsetChange interface
//
rowsetpropset[0].cProperties= 1;
rowsetpropset[0].guidPropertySet= DBPROPSET_ROWSET;
rowsetpropset[0].rgProperties= rowsetprop;
rowsetprop[0].dwPropertyID= DBPROP_IRowsetIndex;
rowsetprop[0].dwOptions= DBPROPOPTIONS_REQUIRED;
rowsetprop[0].colid= DB_NULLID;
rowsetprop[0].vValue.vt= VT_BOOL;
rowsetprop[0].vValue.boolVal= VARIANT_TRUE;
// Open the table using the index
//
hr = pIOpenRowset->OpenRowset(NULL,
&TableID,
&IndexID,
IID_IRowsetIndex,
sizeof(rowsetpropset)/sizeof(rowsetpropset[0]),
rowsetpropset,
(IUnknown**) &pIRowsetIndex);
if(FAILED(hr))
{
goto Exit;
}
// Get IRowset interface
//
hr = pIRowsetIndex->QueryInterface(IID_IRowset, (void**) &pIRowset);
if(FAILED(hr))
{
goto Exit;
}
// Get IColumnsInfo interface
//
hr = pIRowset->QueryInterface(IID_IColumnsInfo, (void **)&pIColumnsInfo);
if(FAILED(hr))
{
goto Exit;
}
// Get the column metadata
//
hr = pIColumnsInfo->GetColumnInfo(&ulNumCols, &pDBColumnInfo, &pStringsBuffer);
if(FAILED(hr) || 0 == ulNumCols)
{
goto Exit;
}
// Create a DBBINDING array.
//
dwBindingSize = sizeof(pwszEmployees)/sizeof(pwszEmployees[0]);
prgBinding = (DBBINDING*)CoTaskMemAlloc(sizeof(DBBINDING)*dwBindingSize);
if (NULL == prgBinding)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Set initial offset for binding position
//
dwOffset = 0;
// Prepare structures to create the accessor
//
for (dwIndex = 0; dwIndex < dwBindingSize; ++dwIndex)
{
if (!GetColumnOrdinal(pDBColumnInfo, ulNumCols, pwszEmployees[dwIndex], &dwOrdinal))
{
hr = E_FAIL;
goto Exit;
}
// Prepare structures to create the accessor
//
prgBinding[dwIndex].iOrdinal= dwOrdinal;
prgBinding[dwIndex].dwPart= DBPART_VALUE | DBPART_STATUS | DBPART_LENGTH;
prgBinding[dwIndex].obLength= dwOffset;
prgBinding[dwIndex].obStatus= prgBinding[dwIndex].obLength + sizeof(ULONG);
prgBinding[dwIndex].obValue= prgBinding[dwIndex].obStatus + sizeof(DBSTATUS);
prgBinding[dwIndex].pTypeInfo= NULL;
prgBinding[dwIndex].pBindExt= NULL;
prgBinding[dwIndex].dwMemOwner= DBMEMOWNER_CLIENTOWNED;
prgBinding[dwIndex].dwFlags= 0;
prgBinding[dwIndex].bPrecision= pDBColumnInfo[dwOrdinal].bPrecision;
prgBinding[dwIndex].bScale= pDBColumnInfo[dwOrdinal].bScale;
switch(pDBColumnInfo[dwOrdinal].wType)
{
case DBTYPE_WSTR:
prgBinding[dwIndex].pObject= NULL;
prgBinding[dwIndex].wType= pDBColumnInfo[dwOrdinal].wType;
prgBinding[dwIndex].cbMaxLen= sizeof(WCHAR)*(pDBColumnInfo[dwOrdinal].ulColumnSize + 1);// Extra buffer for null terminator
break;
default:
prgBinding[dwIndex].pObject= NULL;
prgBinding[dwIndex].wType= pDBColumnInfo[dwOrdinal].wType;
prgBinding[dwIndex].cbMaxLen= pDBColumnInfo[dwOrdinal].ulColumnSize;
break;
}
// Calculate new offset
//
dwOffset = prgBinding[dwIndex].obValue + prgBinding[dwIndex].cbMaxLen;
// Properly align the offset
//
dwOffset = ROUND_UP(dwOffset, COLUMN_ALIGNVAL);
}
// Get IAccessor interface
//
hr = pIRowset->QueryInterface(IID_IAccessor, (void**)&pIAccessor);
if(FAILED(hr))
{
goto Exit;
}
// Create accessor.
//
hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,
dwBindingSize,
prgBinding,
0,
&hAccessor,
NULL);
if(FAILED(hr))
{
goto Exit;
}
// Allocate data buffer for seek and retrieve operation.
//
pData = (BYTE*)CoTaskMemAlloc(dwOffset);
if (NULL == pData)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Set data buffer to zero
//
memset(pData, 0, dwOffset);
// Set data buffer for seek operation
//
wcscpy_s((WCHAR*)(pData+prgBinding[0].obValue),sizeof(L"45gang"), L"45gang");//L"查询内容"
*(ULONG*)(pData+prgBinding[0].obLength)= wcslen((WCHAR*)(pData+prgBindin[0].obValue)*sizeof(WCHAR);
*(DBSTATUS*)(pData+prgBinding[0].obStatus)= DBSTATUS_S_OK;
// Position at a key value within the current range
hr = pIRowsetIndex->Seek(hAccessor, 1, pData, DBSEEK_FIRSTEQ);
if(FAILED(hr))
{
goto exit;
}
// Retrieve a row handle for the row resulting from the seek
//
hr = pIRowset->GetNextRows(DB_NULL_HCHAPTER, 0, 1, &cRowsObtained, &prghRows);
if(FAILED(hr))
{
goto Exit;
}
if (DB_S_ENDOFROWSET != hr)
{
// Fetch actual data
//
hr = pIRowset->GetData(prghRows[0], hAccessor, pData);
if (FAILED(hr))
{
goto Exit;
}
// Update dialog
// If return a null value or status is not OK, ignore the contents of the value and length parts of the buffer.
//
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[0].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[0].obStatus))
{
m_name = (CString)*(WCHAR*)(pData+prgBinding[0].obValue);
}
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[1].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[1].obStatus))
{
m_no = *(WCHAR*)(pData+prgBinding[1].obValue);
}
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[2].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[2].obStatus))
{
m_x = *(WCHAR*)(pData+prgBinding[2].obValue);
}
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[3].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[3].obStatus))
{
m_y = *(WCHAR*)(pData+prgBinding[3].obValue);
}
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[4].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[4].obStatus))
{
m_z = (WCHAR*)(pData+prgBinding[4].obValue);
}
if (DBSTATUS_S_ISNULL != *(DBSTATUS *)(pData+prgBinding[5].obStatus) &&
DBSTATUS_S_OK == *(DBSTATUS *)(pData+prgBinding[5].obStatus))
{
m_p = (WCHAR*)(pData+prgBinding[5].obValue);
}
UpdateData(FALSE);
}
// Release the rowset.
//
pIRowset->ReleaseRows(1, prghRows, NULL, NULL, NULL);
Exit:
//各种释放
m_name m_no m_x m_y m_z m_p
理论得到的值45gang, 19.00, 56456542.26, 4753.6542, 7784.34978, 7875.7453
实际得到的值,4, 1, 5, 4, 7784.34978, 7875.7453
这是为什么呢?
[解决办法]
这里的代码出了问题
m_y = *(WCHAR*)(pData+prgBinding[3].obValue);
应该都是
m_y = (WCHAR*)(pData+prgBinding[3].obValue);