网上只找到VB代码,折腾了不少时间,把代码翻译为VC代码。主要参考这三篇文章:1,2,3。
参数说明:
fileFrom: 外部数据库文件
tableFrom: 外部数据库表
fileTo: 主数据库文件
tableTo: 主数据库文件中链接表的名字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//创建ACCESS数据库链接表
BOOL
CACCADO::CreateLinkedTable(CString fileFrom, CString tableFrom, CString fileTo, CString tableTo)
{
ADOX::_CatalogPtr pCatalog = NULL;
try
{
pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
CString connStr;
connStr = _T(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
) + fileTo + _T(
";"
);
pCatalog->PutActiveConnection(_bstr_t(connStr));
ADOX::_TablePtr pTable = NULL;
pTable.CreateInstance(_T(
"ADOX.Table"
));
pTable->ParentCatalog = pCatalog;
pTable->Name = _bstr_t(tableTo);
ADOX::PropertiesPtr pProperties = pTable->GetProperties();
ADOX::PropertyPtr pProperty = NULL;
pProperty = pProperties->GetItem(_T(
"Jet OLEDB:Create Link"
));
pProperty->Value = TRUE;
pProperty = pProperties->GetItem(_T(
"Jet OLEDB:Link Datasource"
));
pProperty->Value = _bstr_t(fileFrom);
pProperty = pProperties->GetItem(_T(
"Jet OLEDB:Remote Table Name"
));
pProperty->Value = _bstr_t(tableFrom);
pCatalog->Tables->Append(_variant_t((IDispatch*)pTable));
pCatalog->Tables->Refresh();
//刷新
}
catch
(_com_error &e)
{
CString errmsg = _T(
"创建链接表出错:"
) + CString((
LPCSTR
)e.Description());
AfxMessageBox(errmsg);
return
FALSE;
}
return
TRUE;
}
|
相关阅读:
*** walker ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1266095如需转载请自行联系原作者
RQSLT