Connection Close 메서드, Table Type 속성 예제(VC++)
ActiveConnection 속성을 Nothing으로 설정하면 카탈로그를 “닫아야” 합니다. 연결된 컬렉션은 비어 있습니다. 카탈로그의 스키마 개체에서 만든 모든 개체는 분리됩니다. 캐시된 개체의 모든 속성은 계속 사용할 수 있지만 공급자에 대한 호출이 필요한 속성을 읽으려는 시도는 실패합니다.
// BeginCloseConnectionCpp.cpp
// compile with: /EHsc
#import "msado15.dll" rename("EOF", "EndOfFile")
#import "msadox.dll" no_namespace
#include "iostream"
using namespace std;
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void CloseConnectionByNothingX();
void CloseConnectionX();
int main() {
if ( FAILED(::CoInitialize(NULL) ) )
return -1;
CloseConnectionByNothingX();
CloseConnectionX();
::CoUninitialize();
}
void CloseConnectionByNothingX() {
HRESULT hr = S_OK;
// Define and initialize ADOX object pointers, in ADODB namespace.
_CatalogPtr m_pCatalog = NULL;
_TablePtr m_pTable = NULL;
// Define ADODB object pointers
ADODB::_ConnectionPtr m_pCnn = NULL;
// Define other variables
_variant_t vIndex = (short) 0;
try {
TESTHR(hr = m_pCnn.CreateInstance(__uuidof(ADODB::Connection)));
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof(Catalog)));
m_pCnn->Open("Provider='Microsoft.JET.OLEDB.4.0';Data Source= 'c:\\Northwind.mdb';", "", "", NULL);
m_pCatalog->PutActiveConnection(_variant_t((IDispatch *)m_pCnn));
m_pTable = m_pCatalog->Tables->GetItem(vIndex);
// Cache m_pTable.Type info
cout << m_pTable->Type << endl;
_variant_t vCnn;
vCnn.vt = VT_DISPATCH;
vCnn.pdispVal = NULL;
m_pCatalog->PutActiveConnection(vCnn);
// m_pTable is orphaned, will succeed if this was cached
cout << m_pTable->Type << endl;
cout << m_pTable->Columns->GetItem(vIndex)->DefinedSize << endl;
// Previous line will fail if this info has not been cached
}
catch(_com_error &e) {
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nError\n\tSource : %s \n\tdescription : %s \n", (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
}
catch(...) {
cout << "Error occurred in CloseConnectionByNothingX...." << endl;
}
}
void CloseConnectionX() {
HRESULT hr = S_OK;
// Define ADOX object pointers, initialize pointers. These are in ADODB namespace.
_CatalogPtr m_pCatalog = NULL;
_TablePtr m_pTable = NULL;
// Define ADODB object pointers
ADODB::_ConnectionPtr m_pCnn = NULL;
// Define other variables
_variant_t vIndex = (short) 0;
try {
TESTHR(hr = m_pCnn.CreateInstance(__uuidof(ADODB::Connection)));
m_pCnn->Open("Provider='Microsoft.JET.OLEDB.4.0';Data Source= 'c:\\Northwind.mdb';", "", "", NULL);
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof(Catalog)));
m_pCatalog->PutActiveConnection(_variant_t((IDispatch *)m_pCnn));
m_pTable = m_pCatalog->Tables->GetItem(vIndex);
// Cache m_pTable.Type info
cout << m_pTable->Type << endl;
m_pCnn->Close();
// m_pTable is orphaned, will succeed if this was cached
cout << m_pTable->Type << endl;
cout << m_pTable->Columns->GetItem(vIndex)->DefinedSize << endl;
// Previous line will fail if this info has not been cached
}
catch(_com_error &e) {
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nError\n\tSource : %s \n\tdescription : %s \n", (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
}
catch(...) {
cout << "Error occurred in CloseConnectionX...." << endl;
}
}
참고 항목
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기