Keys Append 메서드, Key Type, RelatedColumn, RelatedTable 및 UpdateRule 속성 예제(VC++)
다음 코드에서는 새 외래 키를 만드는 방법을 보여 줍니다. 두 테이블(Customers 및 Orders)이 있다고 가정합니다.
// BeginCreateKeyCpp.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.
_KeyPtr m_pKeyForeign = NULL;
_CatalogPtr m_pCatalog = NULL;
// Define other variables
_bstr_t strcnn("Provider='Microsoft.JET.OLEDB.4.0';Data source = 'c:\\Northwind.mdb';");
try {
TESTHR(hr = m_pKeyForeign.CreateInstance(__uuidof(Key)));
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
m_pCatalog->PutActiveConnection(strcnn);
// Define the foreign key.
m_pKeyForeign->Name = "CustOrder";
m_pKeyForeign->Type = adKeyForeign;
m_pKeyForeign->RelatedTable = "Customers";
m_pKeyForeign->Columns->Append("CustomerId",adVarWChar,0);
m_pKeyForeign->Columns->GetItem("CustomerId")->RelatedColumn = "CustomerId";
m_pKeyForeign->UpdateRule = adRICascade;
// To pass as column parameter to Key's Apppend method
_variant_t vOptional;
vOptional.vt = VT_ERROR;
vOptional.scode = DISP_E_PARAMNOTFOUND;
// Append the foreign key.
m_pCatalog->Tables->GetItem("Orders")->Keys->Append(_variant_t((IDispatch *)m_pKeyForeign),
adKeyPrimary,
vOptional,
L"",
L"");
printf("Foreign key 'CustOrder' is added.\n");
// Delete the key as this is a demonstration.
m_pCatalog->Tables->GetItem("Orders")->Keys->Delete(m_pKeyForeign->Name);
printf("Foreign key 'CustOrder' 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 include files...." << endl;
}
::CoUninitialize();
}
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기