DWORD CCarta::SaveToDB() { char SqlStr[300]; char* sqlinsert = "insert into correspo (Nombre,direccion,fecha_e,tema,sintesis,sintesis2,sintesis3,sintesis4,sintesis5,sindicato,respuesta,provincia,expte) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"; char* sqlupdate = "update correspo set Nombre='%s',direccion='%s',fecha_e='%s',tema='%s',sintesis='%s',sintesis2='%s',sintesis3='%s',sintesis4='%s',sintesis5='%s',sindicato='%s',respuesta='%s',provincia='%s' where expte ='%s'"; char* sql; char fecha[10]; SYSTEMTIME stime; HRESULT hr; ICommandText* pICommandText = NULL; IRowset* pIRowset = NULL; long pRowsCant; if (!m_pIdbDatabase) return 0; stime.wDay = m_FechaEntrada.day; stime.wMonth = m_FechaEntrada.month; stime.wYear = m_FechaEntrada.year; ::GetDateFormat(LOCALE_SYSTEM_DEFAULT,0,&stime,NULL,fecha,300); if (m_ID != 0) sql = sqlupdate; else sql = sqlinsert; for(;;) { sprintf(SqlStr,sql,m_Nombre,m_Direccion,fecha,m_Tema,m_Sintesis1,m_Sintesis2,m_Sintesis3,m_Sintesis4,m_Sintesis5,m_Sindicato,m_Respuesta,m_Provincia,m_Expediente); db_CharToWChar(SqlStr); hr = m_pIdbDatabase->_ICommand()->QueryInterface(IID_ICommandText,(void**)&pICommandText); hr = pICommandText->SetCommandText(DBGUID_DEFAULT,(OLECHAR*)SqlStr); hr = pICommandText->Release(); hr = m_pIdbDatabase->_ICommand()->Execute(NULL,IID_IRowset,NULL,&pRowsCant,(IUnknown **)&pIRowset); break; } return ERR_NONE; }
/******************************************************************** SqlCommandExecuteQuery - executes a SQL command and returns the results if desired NOTE: ppirs and pcRoes are optional ********************************************************************/ extern "C" HRESULT DAPI SqlCommandExecuteQuery( __in IDBCreateCommand* pidbCommand, __in __sql_command LPCWSTR wzSql, __out IRowset** ppirs, __out DBROWCOUNT* pcRows ) { Assert(pidbCommand); HRESULT hr = S_OK; ICommandText* picmdText = NULL; ICommand* picmd = NULL; DBROWCOUNT cRows = 0; if (pcRows) { *pcRows = NULL; } // // create the command // hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd); ExitOnFailure(hr, "failed to create command to execute session"); // // set the sql text into the command // hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText); ExitOnFailure(hr, "failed to get command text object for command"); hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql); ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql); // // execute the command // hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs)); ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql); if (DB_S_ERRORSOCCURRED == hr) { hr = E_FAIL; } if (pcRows) { *pcRows = cRows; } LExit: ReleaseObject(picmd); ReleaseObject(picmdText); return hr; }
DWORD CCarta::LoadExpte() { // preparar consulta DWORD dr; DWORD rc; // Reference count long pRowsCant; HRESULT hr; IRowset* pIRowset = NULL; ICommandText* pICommandText = NULL; CMemArray memarray; DBDATAINFO* dbdatainfo; dr = db_GetdbDataInfo(MAKELONG(EXPTE_CINDEX,CARTAS_TINDEX),this,&dbdatainfo); if (dr != ERR_NONE) return dr; dr = memarray.SetMaxSize(1000); if (dr != ERR_NONE) return dr; sprintf(CorrespoSQL,"select top 1 * from correspo where ucase(%s) = ucase('%s')",dbdatainfo->name,m_Expediente); db_CharToWChar(CorrespoSQL); Clear(); for(;;) { hr = m_pIdbDatabase->_ICommand()->QueryInterface(IID_ICommandText,(void**)&pICommandText); if (hr != S_OK) break; hr = pICommandText->SetCommandText(DBGUID_DEFAULT,(OLECHAR*)CorrespoSQL); if (hr != S_OK) break; hr = m_pIdbDatabase->_ICommand()->Execute(NULL,IID_IRowset,NULL,&pRowsCant,(IUnknown **)&pIRowset); if (hr != S_OK) break; if (pRowsCant == 0) { dr = ERR_NOTFOUND; break; } dr = memarray.SetDBInterfaz(this); dr = memarray.SetData(pIRowset,0); if (memarray.GetRowCount() != 0) memarray.GetData(0); break; }; if (pICommandText) rc = pICommandText->Release(); if (pIRowset) rc = pIRowset->Release(); if ((hr == S_OK) && (dr == ERR_NONE)) return ERR_NONE; if (hr != S_OK) dr = ERR_OLEDBFAIL; return dr; }
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; }
/******************************************************************** SqlSessionExecuteQuery - executes a query and returns the results if desired NOTE: ppirs and pcRoes and pbstrErrorDescription are optional ********************************************************************/ extern "C" HRESULT DAPI SqlSessionExecuteQuery( __in IDBCreateSession* pidbSession, __in __sql_command LPCWSTR wzSql, __out_opt IRowset** ppirs, __out_opt DBROWCOUNT* pcRows, __out_opt BSTR* pbstrErrorDescription ) { Assert(pidbSession); HRESULT hr = S_OK; IDBCreateCommand* pidbCommand = NULL; ICommandText* picmdText = NULL; ICommand* picmd = NULL; DBROWCOUNT cRows = 0; if (pcRows) { *pcRows = NULL; } // // create the command // hr = pidbSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pidbCommand); ExitOnFailure(hr, "failed to create database session"); hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd); ExitOnFailure(hr, "failed to create command to execute session"); // // set the sql text into the command // hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText); ExitOnFailure(hr, "failed to get command text object for command"); hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql); ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql); // // execute the command // hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs)); ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql); if (DB_S_ERRORSOCCURRED == hr) { hr = E_FAIL; } if (pcRows) { *pcRows = cRows; } LExit: if (FAILED(hr) && picmd && pbstrErrorDescription) { HRESULT hrGetErrors = SqlGetErrorInfo(picmd, IID_ICommandText, 0x409, NULL, pbstrErrorDescription); // TODO: use current locale instead of always American-English if (FAILED(hrGetErrors)) { ReleaseBSTR(*pbstrErrorDescription); } } ReleaseObject(picmd); ReleaseObject(picmdText); ReleaseObject(pidbCommand); return hr; }