⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 employees.cpp

📁 EVC下OLEDB方法编写的数据库源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
// Description: Show employee photo.
//
// Notes: This sample only display 24 bit bitmap
//
////////////////////////////////////////////////////////////////////////////////
void Employees::ShowEmployeePhoto()
{
	HDC					hdcMem;
	HDC					hDC;

	// If m_hBitmap is NULL, 
	// clear the photo area with a gray rectangle
	//
	if (NULL == m_hBitmap)
	{
		HBRUSH				hBrush;
		RECT				rect;

		SetRect(&rect, PHOTO_X, PHOTO_Y, PHOTO_X + PHOTO_WIDTH, PHOTO_Y + PHOTO_HEIGHT);

		hDC = GetDC(m_hWndEmployees);
		hBrush = CreateSolidBrush(RGB(192, 192, 192));
		FillRect(hDC, &rect, hBrush);

		DeleteObject(hBrush);
		ReleaseDC(m_hWndEmployees, hDC);

		return;
	}

	// Retrieve the device context handle
	//
	hDC = GetDC(m_hWndEmployees);

	// Creates a memory device context, select bitmap handle
	//
	hdcMem = CreateCompatibleDC(hDC);
	SelectObject(hdcMem, m_hBitmap);

	// Display bitmap,
	//
	BitBlt(	hDC, 
				PHOTO_X, 
				PHOTO_Y, 
				PHOTO_WIDTH,
				PHOTO_HEIGHT, 
				hdcMem, 
				0, 
				0,  
				SRCCOPY); 

	// Delete bitmap object, release the device contexts, 
	//
	DeleteDC(hdcMem);
	ReleaseDC(m_hWndEmployees, hDC);
}


////////////////////////////////////////////////////////////////////////////////
// Function: ClearEmployeeInfo()
//
// Description: clear employee info displayed on the window.
//
// Returns:
//
// Notes:
//
////////////////////////////////////////////////////////////////////////////////
void Employees::ClearEmployeeInfo()
{
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_EMPLOYEE_ID, L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_ADDRESS,     L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_CITY,        L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_REGION,      L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_POSTAL_CODE, L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_COUNTRY,     L"");
	SetDlgItemText(m_hWndEmployees, IDC_EDIT_HOME_PHONE,  L"");

	LoadEmployeePhoto(NULL);
}

////////////////////////////////////////////////////////////////////////////////
// Function: SaveEmployeeInfo()
//
// Description: Save employee info to database.
//
// Returns: NOERROR if succesfull
//
// Notes:
//
////////////////////////////////////////////////////////////////////////////////
HRESULT Employees::SaveEmployeeInfo(DWORD dwEmployeeID)
{
	HRESULT				hr					= NOERROR;			// Error code reporting
	DBBINDING			*prgBinding			= NULL;				// Binding used to create accessor
	HROW				rghRows[1];								// 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[2];							// Used when opening integrated index
   	ULONG				cRowsObtained		= 0;				// Number of rows obtained from the rowset object
	DBCOLUMNINFO		*pDBColumnInfo		= NULL;				// Record column metadata
	BYTE				*pData				= NULL;				// record data
	WCHAR				*pStringsBuffer		= NULL;
	DWORD				dwBindingSize		= 0;
	DWORD				dwIndex				= 0;
	DWORD				dwOffset			= 0;
	DWORD				dwOrdinal			= 0;
    ULONG				ulNumCols;

	IOpenRowset			*pIOpenRowset		= NULL;				// Provider Interface Pointer
	IRowset				*pIRowset			= NULL;				// Provider Interface Pointer
    IRowsetChange		*pIRowsetChange		= NULL;
	IRowsetIndex		*pIRowsetIndex		= NULL;				// Provider Interface Pointer
	IAccessor			*pIAccessor			= NULL;				// Provider Interface Pointer
	IColumnsInfo		*pIColumnsInfo		= NULL;				// Provider Interface Pointer
	HACCESSOR			hAccessor			= DB_NULL_HACCESSOR;// Accessor handle

	WCHAR*				pwszEmployees[]		=	{				// Employee info column names
													L"EmployeeID",
													L"Address",
													L"City",
													L"Region",
													L"PostalCode",
													L"Country",
													L"HomePhone"
												};
	
	VariantInit(&rowsetprop[0].vValue);
	VariantInit(&rowsetprop[1].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*)TABLE_EMPLOYEE;

	IndexID.eKind			= DBKIND_NAME;
	IndexID.uName.pwszName	= L"PK_Employees";

	// Request ability to use IRowsetChange interface
	// 
	rowsetpropset[0].cProperties	= 2;
	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;

	rowsetprop[1].dwPropertyID		= DBPROP_IRowsetIndex;
	rowsetprop[1].dwOptions			= DBPROPOPTIONS_REQUIRED;
	rowsetprop[1].colid				= DB_NULLID;
	rowsetprop[1].vValue.vt			= VT_BOOL;
	rowsetprop[1].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;
	}

	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.
	//
	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;
		}

		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].pObject		= NULL;
		prgBinding[dwIndex].pBindExt	= NULL;
		prgBinding[dwIndex].dwMemOwner	= DBMEMOWNER_CLIENTOWNED;
		prgBinding[dwIndex].dwFlags		= 0;
		prgBinding[dwIndex].wType		= pDBColumnInfo[dwOrdinal].wType;
		prgBinding[dwIndex].bPrecision	= pDBColumnInfo[dwOrdinal].bPrecision;
		prgBinding[dwIndex].bScale		= pDBColumnInfo[dwOrdinal].bScale;

		switch(prgBinding[dwIndex].wType)
		{
		case DBTYPE_WSTR:		
			prgBinding[dwIndex].cbMaxLen = sizeof(WCHAR)*(pDBColumnInfo[dwOrdinal].ulColumnSize + 1);	// Extra buffer for null terminator 
			break;
		default:
			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
    //
	*(ULONG*)(pData+prgBinding[0].obLength)		= 4;
	*(DBSTATUS*)(pData+prgBinding[0].obStatus)	= DBSTATUS_S_OK;
	*(int*)(pData+prgBinding[0].obValue)		= dwEmployeeID;

	// 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)
	{
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_ADDRESS,	  (WCHAR*)(pData+prgBinding[1].obValue), prgBinding[1].cbMaxLen);
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_CITY,		  (WCHAR*)(pData+prgBinding[2].obValue), prgBinding[2].cbMaxLen);
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_REGION,	  (WCHAR*)(pData+prgBinding[3].obValue), prgBinding[3].cbMaxLen);
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_POSTAL_CODE, (WCHAR*)(pData+prgBinding[4].obValue), prgBinding[4].cbMaxLen);
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_COUNTRY,	  (WCHAR*)(pData+prgBinding[5].obValue), prgBinding[5].cbMaxLen);
		GetDlgItemText(m_hWndEmployees, IDC_EDIT_HOME_PHONE,  (WCHAR*)(pData+prgBinding[6].obValue), prgBinding[6].cbMaxLen);

		for (dwIndex = 1; dwIndex <= 6; ++dwIndex)
		{
			*(ULONG*)(pData+prgBinding[dwIndex].obLength)	 = wcslen((WCHAR*)(pData+prgBinding[dwIndex].obValue))*sizeof(WCHAR);
			*(DBSTATUS*)(pData+prgBinding[dwIndex].obStatus) = DBSTATUS_S_OK;
		}

		// Set data to database
		//
		hr = pIRowsetChange->SetData(prghRows[0], hAccessor, pData);
	}

	// Release the rowset.
	//
	pIRowset->ReleaseRows(1, prghRows, NULL, NULL, NULL);

Exit:
    // Clear Variants
    //
	VariantClear(&rowsetprop[0].vValue);
	VariantClear(&rowsetprop[1].vValue);

    // Free allocated memory for row handles.
    //
	if (prghRows)
	{
        CoTaskMemFree(prghRows);
        prghRows = NULL;
	}

    // 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(pIAccessor)
	{
		pIAccessor->ReleaseAccessor(hAccessor, NULL); 
		pIAccessor->Release();
	}

	if (pIColumnsInfo)
	{
		pIColumnsInfo->Release();
	}

	if (pIRowsetChange)
	{
		pIRowsetChange->Release();
	}

	if(pIRowset)
	{
		pIRowset->Release();
	}

	if (pIRowsetIndex)
	{
		pIRowsetIndex->Release();
	}

	if(pIOpenRowset)
	{
		pIOpenRowset->Release();
	}

	return hr;
}

////////////////////////////////////////////////////////////////////////////////
// Function: GetColumnOrdinal()
//
// Description: Returns column ordinal for column name.
//
// Parameters
//		pDBColumnInfo	- a pointer to Database column info
//		dwNumCols		- number of columns
//		pwszColName		- column name
//		pOrdinal		- column ordinal
//
// Returns: TRUE if succesfull
//
////////////////////////////////////////////////////////////////////////////////
BOOL Employees::GetColumnOrdinal(DBCOLUMNINFO* pDBColumnInfo, DWORD dwNumCols, WCHAR* pwszColName, DWORD* pOrdinal)
{
	for(DWORD dwCol = 0; dwCol< dwNumCols; ++dwCol)
	{
		if(NULL != pDBColumnInfo[dwCol].pwszName)
		{
			if(0 == _wcsicmp(pDBColumnInfo[dwCol].pwszName, pwszColName))
			{
				*pOrdinal = pDBColumnInfo[dwCol].iOrdinal;
				return TRUE;
			}
		}
	}

	return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -