实现连接方法如下:
BOOL GetArcSDEWorkspace(CString server, CString port, CString database, CString user, CString pass, CString version, IWorkspacePtr &outWorkspace)
{
outWorkspace = NULL;
IPropertySetPtr ipPropertySet;
HRESULT hr = ipPropertySet.CreateInstance(CLSID_PropertySet);
if (FAILED(hr))
{
CString strValue;
strValue.Format(_T("无法连接空间数据库引擎参数接口IPropertySetPtr。返回错误信息是:%ld。"), hr);
MessageBox(NULL, strValue, _T("提示"), MB_OK | MB_ICONWARNING);
return FALSE;
}
bool bDirct = false;
CString strServer = server, strInstance = port, strDb = database, strUser = user, strPwd = pass, strVersion = version;
int nCount = port.GetLength();
if (nCount > 4 && port.Left(4).CollateNoCase(_T("sde:")) == 0)
{
bDirct = true;
//strServer = _T("");
//strPwd.Format(_T("%s@%s/%s"),pass,server,database);
strInstance.Format(_T("%s:%s/%s"), port, server, strDb);
}
// configure the propertyset
else
{
hr = ipPropertySet->SetProperty(CComBSTR(L"SERVER"), CComVariant(strServer));
hr = ipPropertySet->SetProperty(CComBSTR(L"DATABASE"), CComVariant(strDb));
}
hr = ipPropertySet->SetProperty(CComBSTR(L"INSTANCE"), CComVariant(strInstance));
hr = ipPropertySet->SetProperty(CComBSTR(L"USER"), CComVariant(strUser));
hr = ipPropertySet->SetProperty(CComBSTR(L"PASSWORD"), CComVariant(strPwd));
hr = ipPropertySet->SetProperty(CComBSTR(L"VERSION"), CComVariant(strVersion));
// open workspace
IWorkspaceFactoryPtr ipWorkspaceFactory;
hr = ipWorkspaceFactory.CreateInstance(CLSID_SdeWorkspaceFactory);
if (FAILED(hr))
{
CString strValue;
strValue.Format(_T("无法连接空间数据库引擎参数接口IWorkspaceFactoryPtr.返回错误信息是:%ld。"), hr);
MessageBox(NULL, strValue, _T("提示"), MB_OK | MB_ICONWARNING);
return FALSE;
}
hr = ipWorkspaceFactory->Open(ipPropertySet, NULL, &outWorkspace);
if (FAILED(hr))
{
if (bDirct)
{
if (port.CompareNoCase(_T("sde:oracle11g")) == 0)
strInstance.Format(_T("%s:%s/%s"), _T("sde:oracle10g"), server, strDb);
else if (port.CompareNoCase(_T("sde:oracle10g")) == 0)
strInstance.Format(_T("%s:%s/%s"), _T("sde:oracle11g"), server, strDb);
else
{
MessageBox(NULL, _T("实例名错误10g或11g"), _T("提示"), MB_OK | MB_ICONWARNING);
}
hr = ipPropertySet->SetProperty(CComBSTR(_T("INSTANCE")), CComVariant(strInstance));
hr = ipWorkspaceFactory->Open(ipPropertySet, NULL, &outWorkspace);
if (FAILED(hr))
{
}
}
}
if (FAILED(hr))
{
CString strValue;
strValue.Format(_T("无法连接空间数据库引擎参数接口IWorkspacePtr。返回错误信息是:%ld。"), hr);
if (FDO_E_SE_IOMGR_NOT_AVAILABLE == hr)
strValue.Format(_T("请检查SDE服务状态。错误号是:%ld。"), hr);
MessageBox(NULL, strValue, _T("提示"), MB_OK | MB_ICONWARNING);
return FALSE;
}
return (NULL != outWorkspace);
}
连接参数示例1(按实例连接):
SERVER = 127.0.0.1
INSTANCE = sde:oracle11g
DATABASE=orcl
USER = SDE
PASSWORD = SDE
VERSION = SDE.DEFAULT
连接参数示例2(按服务连接):
SERVER = sde.xxxx.com
INSTANCE = 5151
DATABASE=
USER = tempuser
PASSWORD = abcabc
VERSION = SDE.DEFAULT