|
|
|
OLE DB
|
|||
|---|---|---|---|
|
#18+
Задача вывода названия столбцов БД, вроде всё правильно но она вылетает, пишу на VS.NET 2003 #define UNICODE #define _UNICODE #define DBINITCONSTANTS #define INITGUID #include <iostream> #include <Windows.h> #include <stdio.h> #include <tchar.h> #include <stddef.h> #include <oledb.h> #include <oledberr.h> #include <msdaguid.h> #include <msdasql.h> void main() { IDBInitialize* pIDBInitialize = NULL; IDBCreateSession* pCreateSession = NULL; IDBCreateCommand* pCreateCommand = NULL; IRowset* pRowset = NULL; ICommandText* pCommandText = NULL; IDBProperties* pIDBProperties; IColumnsInfo *pColumnsInfo; IAccessor* pAccessor; HACCESSOR hAccessor; DBCOLUMNINFO* pDBColumnInfo; DBPROP InitProperties[4]; DBPROPSET rgInitPropSet[1]; INT i; ULONG j, lNumCols, cbColOffset = 0; ULONG lNumRowsRetrieved; HROW hRows[5]; HROW* pRows = &hRows[0]; LONG cNumRows; DBBINDING* pBindings; char *pBuffer; WCHAR* pStringBuffer; char *p = new char[100]; LPCOLESTR wCmdString = OLESTR("Select * From Table1"); CoInitialize(NULL); CoCreateInstance(CLSID_MSDASQL, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**)&pIDBInitialize); for(i = 0; i < 4; i++) { VariantInit(&InitProperties .vValue); InitProperties.dwOptions = DBPROPOPTIONS_REQUIRED; InitProperties.colid = DB_NULLID; } InitProperties[0].dwPropertyID = DBPROP_INIT_PROMPT; InitProperties[0].vValue.vt = VT_I2; InitProperties[0].vValue.iVal = DBPROMPT_NOPROMPT; InitProperties[1].dwPropertyID = DBPROP_AUTH_USERID; InitProperties[1].vValue.vt = VT_BSTR; InitProperties[1].vValue.bstrVal = SysAllocString((LPOLESTR)L""); InitProperties[2].dwPropertyID = DBPROP_AUTH_PASSWORD; InitProperties[2].vValue.vt = VT_BSTR; InitProperties[2].vValue.bstrVal = SysAllocString((LPOLESTR)L""); InitProperties[3].dwPropertyID = DBPROP_INIT_DATASOURCE; InitProperties[3].vValue.vt = VT_BSTR; InitProperties[3].vValue.bstrVal = SysAllocString((LPOLESTR)L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\data.mdb;"); rgInitPropSet[0].guidPropertySet = DBPROPSET_DBINIT; rgInitPropSet[0].cProperties = 4; rgInitPropSet[0].rgProperties = InitProperties; pIDBInitialize->QueryInterface(IID_IDBProperties, (void**)&pIDBProperties); pIDBProperties->SetProperties(1, rgInitPropSet); pIDBProperties->Release(); pIDBInitialize->Initialize(); pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pCreateSession); ////////////////////////////////////////////////////////////////////////// //вот в этой строке она вылетает. pCreateSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pCreateCommand); ////////////////////////////////////////////////////////////////////////// pCreateCommand->CreateCommand(NULL, IID_ICommandText, (IUnknown**)&pCommandText); pCommandText->SetCommandText(DBGUID_DBSQL, wCmdString); pCommandText->Execute(NULL, IID_IRowset, NULL, &cNumRows, (IUnknown**)&pRowset); pRowset->QueryInterface(IID_IColumnsInfo, (void**)&pColumnsInfo); pColumnsInfo->GetColumnInfo(&lNumCols, &pDBColumnInfo, &pStringBuffer); pColumnsInfo->Release(); pBindings = new DBBINDING[lNumCols]; for(j = 0; j < lNumCols; j++) { pBindings[j].iOrdinal = j + 1; pBindings[j].obValue = cbColOffset; pBindings[j].obLength = 0; pBindings[j].obStatus = 0; pBindings[j].pTypeInfo = NULL; pBindings[j].pObject = NULL; pBindings[j].pBindExt = NULL; pBindings[j].dwPart = DBPART_VALUE; pBindings[j].dwMemOwner = DBMEMOWNER_CLIENTOWNED; pBindings[j].eParamIO = DBPARAMIO_NOTPARAM; pBindings[j].cbMaxLen = pDBColumnInfo[j].ulColumnSize; pBindings[j].dwFlags = 0; pBindings[j].wType = pDBColumnInfo[j].wType; pBindings[j].bPrecision = pDBColumnInfo[j].bPrecision; pBindings[j].bScale = pDBColumnInfo[j].bScale; cbColOffset = cbColOffset + pDBColumnInfo[j].ulColumnSize; } pRowset->QueryInterface(IID_IAccessor, (void**)&pAccessor); pAccessor->CreateAccessor(DBACCESSOR_ROWDATA, lNumCols, pBindings, 0, &hAccessor, NULL); for(j = 0; j < lNumCols; j++) { WideCharToMultiByte(CP_OEMCP, 0, pDBColumnInfo[j].pwszName, -1, p, 100, 0, 0); std::cout<<"\t"<<p; } delete pBuffer; delete p; pAccessor->ReleaseAccessor(hAccessor, NULL); pAccessor->Release(); delete pBindings; pRowset->Release(); pCommandText->Release(); pCreateSession->Release(); pCreateCommand->Release(); pIDBInitialize->Uninitialize(); pIDBInitialize->Release(); CoUninitialize(); } ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 20.12.2004, 20:18 |
|
||
|
OLE DB
|
|||
|---|---|---|---|
|
#18+
Проще надо - попроще. Сначала подключение/отключение - затем запрос - затем только дальше ковыряйся. И по порядку отлаживать. А так - в таком огромном участке кода - кто же тебе ошибку то найдет. Вот ты и сам то небось запутался. PS. А если действовать по инструкции (BOL) - то все работает именно так. как там написано. ( Я проверял ) ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 20.12.2004, 21:18 |
|
||
|
|

start [/forum/topic.php?fid=57&msg=32835864&tid=2033960]: |
0ms |
get settings: |
10ms |
get forum list: |
20ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
91ms |
get topic data: |
13ms |
get forum data: |
3ms |
get page messages: |
55ms |
get tp. blocked users: |
1ms |
| others: | 241ms |
| total: | 442ms |

| 0 / 0 |
