Exemple #1
0
DWORD CTextildb::Open()
{
	SetCursor(LoadCursor(NULL,IDC_WAIT));
	DWORD					dr;
	DWORD					rc;
	HRESULT					hr;
	IDataInitialize *		pIDataInitialize    = NULL;
	IDBInitialize *			pIDBInitialize      = NULL;
	IDBCreateSession*		pIDBCreateSession	= NULL;
	IUnknown *				pUnkSession			= NULL;
	ICommandText*			pICommandText		= NULL;
	IDBCreateCommand*		pICreateCommand		= NULL;
	char					cConnectionStr[500];
	strcpy(cConnectionStr,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bd1.mdb;Persist Security Info=False");
	db_CharToWChar(cConnectionStr);
	for(;;)
	{
		dr = ERR_OLEDBFAIL;
		hr = CoCreateInstance(CLSID_MSDAINITIALIZE,NULL,CLSCTX_INPROC_SERVER,IID_IDataInitialize,(VOID **)&pIDataInitialize);
		if (hr != S_OK) break;
		hr = pIDataInitialize->GetDataSource(NULL,CLSCTX_INPROC_SERVER,(LPCOLESTR)cConnectionStr,IID_IDBInitialize,(IUnknown **)&pIDBInitialize);
		if (hr != S_OK) break;
		hr = pIDBInitialize->Initialize();
		if (hr != S_OK) break;
		hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pIDBCreateSession);
		if (hr != S_OK) break;
		hr = pIDBCreateSession->CreateSession(NULL,IID_IOpenRowset,&pUnkSession );
		if (hr != S_OK) break;
		hr = pUnkSession->QueryInterface(IID_IDBCreateCommand,(void**)&pICreateCommand);
		if (hr != S_OK) break;
		hr = pICreateCommand->CreateCommand(NULL,IID_ICommand,(IUnknown**)&m_lpICommand);
		if (hr != S_OK) break;
		dr = ERR_NONE;
		break;
	};
	if (pIDataInitialize)	rc = pIDataInitialize->Release();
	if (pIDBInitialize)		rc = pIDBInitialize->Release();
	if (pIDBCreateSession)	rc = pIDBCreateSession->Release();
	if (pUnkSession)		rc = pUnkSession->Release();
	if (pICreateCommand)	rc = pICreateCommand->Release();
	if (hr != S_OK) 
	{
		if (m_lpICommand)		rc = m_lpICommand->Release();
		m_lpICommand = NULL;
	}
	SetCursor(LoadCursor(NULL,IDC_ARROW));
	return ERR_OLEDBFAIL;
}
HRESULT GetDBSessionFromDataSource
(
 IDBInitialize*      pIDBInitialize,      // [in]
 IOpenRowset**       ppIOpenRowset_out    // [out]
 )
{
	IDBCreateSession*   pIDBCreateSession;
	IOpenRowset*        pIOpenRowset;
	HRESULT             hr;


	DumpStatusMsg( "Getting a DBSession object from the data source object...\n" );

	assert(pIDBInitialize != NULL);
	assert(ppIOpenRowset_out  != NULL );

	hr = pIDBInitialize->QueryInterface( IID_IDBCreateSession, (void**)&pIDBCreateSession);
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "IDBInitialize::QI for IDBCreateSession");
		goto error;
	}

	hr = pIDBCreateSession->CreateSession( NULL, IID_IOpenRowset, (IUnknown**)&pIOpenRowset );    
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "IDBCreateSession::CreateSession");
		goto error;
	}
	pIDBCreateSession->Release();
	pIDBCreateSession = NULL;

	// all went well
	*ppIOpenRowset_out = pIOpenRowset;
	return ResultFromScode( S_OK );

error:
	if (pIDBCreateSession)
		pIDBCreateSession->Release();

	*ppIOpenRowset_out = NULL;    
	return ResultFromScode( hr );
}
ICommandText* CClientDB::CreateCommand( IDBInitialize* pIDBInitialize ,LPCWSTR wCmdString )
{
 	IDBCreateSession*   pIDBCreateSession       = NULL;
	IDBCreateCommand*   pIDBCreateCommand       = NULL;
	ICommandText*		pICommandText			= NULL;

	if( pIDBInitialize == NULL || wCmdString == NULL )
	{
		return NULL;
	}

	//Create a new activity from the data source object.
    if(FAILED(pIDBInitialize->QueryInterface( IID_IDBCreateSession, (void**) &pIDBCreateSession)))
    {
		return NULL;
    }

    if(FAILED(pIDBCreateSession->CreateSession( NULL, IID_IDBCreateCommand, (IUnknown**) &pIDBCreateCommand)))
    {
		return NULL;
    }
    pIDBCreateSession->Release();    

    //Create a Command object.
    if(FAILED(pIDBCreateCommand->CreateCommand( NULL, IID_ICommandText, (IUnknown**) &pICommandText)))
    {
        pIDBCreateCommand->Release();
		return NULL;
    }
    pIDBCreateCommand->Release();
  
    //Set the command text.
    if(FAILED(pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString )))
    {
		pICommandText->Release();
		return NULL;
    }
	return pICommandText;
}