?? employees.cpp
字號:
// Release interfaces
//
if(pIDBProperties)
{
pIDBProperties->Release();
}
if (pIDBInitialize)
{
pIDBInitialize->Release();
}
return hr;
}
////////////////////////////////////////////////////////////////////////////////
// Function: ExecuteSQL
//
// Description:
// Executes a non row returning SQL statement
//
// Parameters
// pICmdText - a pointer to the ICommandText interface on the Command Object
// pwszQuery - the SQL statement to execute
//
// Returns: NOERROR if succesfull
//
////////////////////////////////////////////////////////////////////////////////
HRESULT Employees::ExecuteSQL(ICommandText *pICmdText, WCHAR * pwszQuery)
{
HRESULT hr = NOERROR;
hr = pICmdText->SetCommandText(DBGUID_SQL, pwszQuery);
if(FAILED(hr))
{
goto Exit;
}
hr = pICmdText->Execute(NULL, IID_NULL, NULL, NULL, NULL);
Exit:
return hr;
}
////////////////////////////////////////////////////////////////////////////////
// Function: InsertEmployeeInfo
//
// Description: Inserts sample data
//
// Returns: NOERROR if succesfull
//
////////////////////////////////////////////////////////////////////////////////
HRESULT Employees::InsertEmployeeInfo()
{
HRESULT hr = NOERROR; // Error code reporting
DBBINDING *prgBinding = NULL; // Binding used to create accessor
HROW rghRows[1] = {DB_NULL_HROW}; // Array of row handles obtained from the rowset object
HROW *prghRows = rghRows; // Row handle(s) pointer
DBID TableID; // Used to open/create table
DBID IndexID; // Used to create index
DBPROPSET rowsetpropset[1]; // Used when opening integrated index
DBPROP rowsetprop[1]; // Used when opening integrated index
ULONG cRowsObtained = 0; // Number of rows obtained from the rowset object
DBOBJECT dbObject; // DBOBJECT data.
DBCOLUMNINFO *pDBColumnInfo = NULL; // Record column metadata
BYTE *pData = NULL; // record data
WCHAR *pStringsBuffer = NULL;
DWORD dwBindingSize = 0;
DWORD dwIndex = 0;
DWORD dwRow = 0;
DWORD dwCol = 0;
DWORD dwOffset = 0;
ULONG ulNumCols;
IOpenRowset *pIOpenRowset = NULL; // Provider Interface Pointer
IRowset *pIRowset = NULL; // Provider Interface Pointer
ITransactionLocal *pITxnLocal = NULL; // Provider Interface Pointer
IRowsetChange *pIRowsetChange = NULL; // Provider Interface Pointer
IAccessor *pIAccessor = NULL; // Provider Interface Pointer
ISequentialStream *pISequentialStream = NULL; // Provider Interface Pointer
IColumnsInfo *pIColumnsInfo = NULL; // Provider Interface Pointer
HACCESSOR hAccessor = DB_NULL_HACCESSOR;// Accessor handle
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;
}
hr = pIOpenRowset->QueryInterface(IID_ITransactionLocal, (void**)&pITxnLocal);
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*)TABLE_EMPLOYEE;
IndexID.eKind = DBKIND_NAME;
IndexID.uName.pwszName = L"PK_Employees";
// Request ability to use IRowsetChange interface
//
rowsetpropset[0].cProperties = 1;
rowsetpropset[0].guidPropertySet= DBPROPSET_ROWSET;
rowsetpropset[0].rgProperties = rowsetprop;
rowsetprop[0].dwPropertyID = DBPROP_IRowsetChange;
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_IRowset,
sizeof(rowsetpropset)/sizeof(rowsetpropset[0]),
rowsetpropset,
(IUnknown**) &pIRowset);
if(FAILED(hr))
{
goto Exit;
}
// Get IRowsetChange interface
//
hr = pIRowset->QueryInterface(IID_IRowsetChange, (void**)&pIRowsetChange);
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.
// The binding doesn't include the bookmark column (first column).
//
dwBindingSize = ulNumCols - 1;
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)
{
prgBinding[dwIndex].iOrdinal = pDBColumnInfo[dwIndex + 1].iOrdinal;
prgBinding[dwIndex].pTypeInfo = NULL;
prgBinding[dwIndex].pBindExt = NULL;
prgBinding[dwIndex].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
prgBinding[dwIndex].dwFlags = 0;
prgBinding[dwIndex].bPrecision = pDBColumnInfo[dwIndex + 1].bPrecision;
prgBinding[dwIndex].bScale = pDBColumnInfo[dwIndex + 1].bScale;
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);
switch(pDBColumnInfo[dwIndex + 1].wType)
{
case DBTYPE_BYTES:
// Set up the DBOBJECT structure.
//
dbObject.dwFlags = STGM_WRITE;
dbObject.iid = IID_ISequentialStream;
prgBinding[dwIndex].pObject = &dbObject;
prgBinding[dwIndex].cbMaxLen = sizeof(IUnknown*);
prgBinding[dwIndex].wType = DBTYPE_IUNKNOWN;
break;
case DBTYPE_WSTR:
prgBinding[dwIndex].pObject = NULL;
prgBinding[dwIndex].wType = pDBColumnInfo[dwIndex + 1].wType;
prgBinding[dwIndex].cbMaxLen = sizeof(WCHAR)*(pDBColumnInfo[dwIndex + 1].ulColumnSize + 1); // Extra buffer for null terminator
break;
default:
prgBinding[dwIndex].pObject = NULL;
prgBinding[dwIndex].wType = pDBColumnInfo[dwIndex + 1].wType;
prgBinding[dwIndex].cbMaxLen = pDBColumnInfo[dwIndex + 1].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;
}
// Begins a new local transaction
//
hr = pITxnLocal->StartTransaction(ISOLATIONLEVEL_READCOMMITTED | ISOLATIONLEVEL_CURSORSTABILITY, 0, NULL, NULL);
if(FAILED(hr))
{
goto Exit;
}
// Insert sample data
//
for (dwRow = 0; dwRow < sizeof(g_SampleEmployeeData)/sizeof(g_SampleEmployeeData[0]); ++dwRow)
{
DWORD dwPhotoCol;
DWORD dwInfoSize;
LPWSTR lpwszInfo;
// Set data buffer to zero
//
memset(pData, 0, dwOffset);
for (dwCol = 0; dwCol < dwBindingSize; ++dwCol)
{
// Get column value in string
//
lpwszInfo = g_SampleEmployeeData[dwRow].wszEmployeeInfo[dwCol];
switch(prgBinding[dwCol].wType)
{
case DBTYPE_WSTR:
// Copy value to binding buffer, truncate the string if it is too long
//
dwInfoSize = prgBinding[dwCol].cbMaxLen/sizeof(WCHAR) - 1;
if (wcslen(lpwszInfo) >= dwInfoSize)
{
wcsncpy((WCHAR*)(pData+prgBinding[dwCol].obValue), lpwszInfo, dwInfoSize);
*(WCHAR*)(pData+prgBinding[dwCol].obValue+dwInfoSize*sizeof(WCHAR)) = WCHAR('\0');
}
else
{
wcscpy((WCHAR*)(pData+prgBinding[dwCol].obValue), lpwszInfo);
}
*(ULONG*)(pData+prgBinding[dwCol].obLength) = wcslen((WCHAR*)(pData+prgBinding[dwCol].obValue))*sizeof(WCHAR);
*(DBSTATUS*)(pData+prgBinding[dwCol].obStatus) = DBSTATUS_S_OK;
break;
case DBTYPE_IUNKNOWN:
dwPhotoCol = dwCol;
break;
case DBTYPE_I4:
*(int*)(pData+prgBinding[dwCol].obValue) = _wtoi(g_SampleEmployeeData[dwRow].wszEmployeeInfo[dwCol]);
*(ULONG*)(pData+prgBinding[dwCol].obLength) = 4;
*(DBSTATUS*)(pData+prgBinding[dwCol].obStatus) = DBSTATUS_S_OK;
break;
default:
break;
}
}
// Insert data to database
//
hr = pIRowsetChange->InsertRow(DB_NULL_HCHAPTER, hAccessor, pData, prghRows);
if (FAILED(hr))
{
goto Abort;
}
// Get the row data
//
hr = pIRowset->GetData(rghRows[0], hAccessor, pData);
if(FAILED(hr))
{
goto Abort;
}
// Check the status
//
if (DBSTATUS_S_OK != *(DBSTATUS*)(pData+prgBinding[dwPhotoCol].obStatus))
{
hr = E_FAIL;
goto Abort;
}
// Insert photo into database through ISequentialStream
//
pISequentialStream = (*(ISequentialStream**) (pData + prgBinding[dwPhotoCol].obValue));
if (pISequentialStream)
{
// Insert photo
//
hr = SaveEmployeePhoto(pISequentialStream, g_SampleEmployeeData[dwRow].dwEmployeePhoto);
if(FAILED(hr))
{
goto Abort;
}
// Release ISequentialStream interface
//
hr = pISequentialStream->Release();
if(FAILED(hr))
{
pISequentialStream = NULL;
goto Abort;
}
pISequentialStream = NULL;
}
// Release the rowset
//
hr = pIRowset->ReleaseRows(1, prghRows, NULL, NULL, NULL);
if(FAILED(hr))
{
goto Abort;
}
prghRows[0] = DB_NULL_HROW;
}
// Commit the transaction
//
if (pITxnLocal)
{
pITxnLocal->Commit(FALSE, XACTTC_SYNC, 0);
}
goto Exit;
Abort:
if (DB_NULL_HROW != prghRows[0])
{
pIRowset->ReleaseRows(1, prghRows, NULL, NULL, NULL);
}
// Abort the transaction
//
if (pITxnLocal)
{
pITxnLocal->Abort(NULL, FALSE, FALSE);
}
Exit:
// Clear Variants
//
VariantClear(&rowsetprop[0].vValue);
// Free allocated DBBinding memory
//
if (prgBinding)
{
CoTaskMemFree(prgBinding);
prgBinding = NULL;
}
// Free allocated column info memory
//
if (pDBColumnInfo)
{
CoTaskMemFree(pDBColumnInfo);
pDBColumnInfo = NULL;
}
// Free allocated column string values buffer
//
if (pStringsBuffer)
{
CoTaskMemFree(pStringsBuffer);
pStringsBuffer = NULL;
}
// Free data record buffer
//
if (pData)
{
CoTaskMemFree(pData);
pData = NULL;
}
// Release interfaces
//
if(pISequentialStream)
{
pISequentialStream->Release();
}
if(pIAccessor)
{
pIAccessor->ReleaseAccessor(hAccessor, NULL);
pIAccessor->Release();
}
if (pIColumnsInfo)
{
pIColumnsInfo->Release();
}
if (pIRowsetChange)
{
pIRowsetChange->Release();
}
if (pITxnLocal)
{
pITxnLocal->Release();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -