ParentCatalog 속성 예제(VC++)
다음 코드에서는 카탈로그에 테이블을 추가하기 전에 ParentCatalog 속성을 사용하여 공급자별 속성에 액세스하는 방법을 보여 줍니다. 이 속성은 Microsoft Jet 데이터베이스에 AutoIncrement 필드를 만드는 AutoIncrement입니다.
// BeginCreateAutoIncrColumnCpp.cpp
// compile with: /EHsc
#import "msado15.dll" rename("EOF", "EndOfFile")
#import "msadox.dll" no_namespace
#include "iostream"
using namespace std;
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
int main() {
if ( FAILED(::CoInitialize(NULL)) )
return -1;
HRESULT hr = S_OK;
// Define and initialize ADOX object pointers. These are in ADODB namespace.
_CatalogPtr m_pCatalog = NULL;
_TablePtr m_pTable = NULL;
// Define ADODB object pointers.
ADODB::_ConnectionPtr m_pCnn = NULL;
// Define string variables
_bstr_t strCnn("Provider='Microsoft.JET.OLEDB.4.0';Data Source='c:\\Northwind.mdb';");
try {
TESTHR(hr = m_pCnn.CreateInstance(__uuidof(ADODB::Connection)));
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
TESTHR(hr = m_pTable.CreateInstance(__uuidof (Table)));
// Connect the catalog.
m_pCnn->Open (strCnn, "", "", NULL);
m_pCatalog->PutActiveConnection(variant_t((IDispatch *)m_pCnn));
m_pTable->Name = "MyContacts";
m_pTable->ParentCatalog = m_pCatalog;
// Create fields and append them to the new Table object.
m_pTable->Columns->Append("ContactId", adInteger,0);
// Make the ContactId column and auto incrementing column
m_pTable->Columns->GetItem("ContactId")->Properties->GetItem("AutoIncrement")->Value = true;
m_pTable->Columns->Append("CustomerID", adVarWChar,0);
m_pTable->Columns->Append("FirstName", adVarWChar,0);
m_pTable->Columns->Append("LastName", adVarWChar,0);
m_pTable->Columns->Append("Phone", adVarWChar, 20);
m_pTable->Columns->Append("Notes", adLongVarWChar,0);
m_pCatalog->Tables->Append(_variant_t((IDispatch*)m_pTable));
// Refresh the database.
m_pCatalog->Tables->Refresh();
printf("Table 'MyContacts' is added.\n");
// Delete new table, since this is only an example
m_pCatalog->Tables->Delete("MyContacts");
printf("Table 'MyContacts' is deleted.\n");
}
catch(_com_error &e) {
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n\tSource : %s \n\tdescription : %s \n ", (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
}
catch(...) {
cout << "Error occurred in CreateAutoIncrColumnX...."<< endl;
}
m_pCatalog = NULL;
::CoUninitialize();
}