// If connection object is NULL, user cancelled. if ( NULL == m_pConnection ) return "";
// Open connection (connection returned by DataLinks is just // a holder for the returned ConnectionString). TESTHR(hr = m_pConnection->Open( m_pConnection->ConnectionString, L"", L"", -1 ) ); strCnnString =(char *) (m_pConnection->ConnectionString); return strCnnString; } catch( _com_error &e){ HandleComError(e); // executing the query. if(m_pConnection != NULL) m_pConnection.Release(); return ""; }
就是读到 数据链接属性对话框 里面设置的变量
组成一个 数据库链接字符串
csdn居然没有人会么?
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
inline void HandleComError(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bs = _bstr_t(" 错误: ") + _bstr_t(e.Error())
+ _bstr_t(" ")
+ _bstr_t(" 消息: ") + _bstr_t(e.ErrorMessage())
+ _bstr_t(" ")
+ _bstr_t(" 错误描述: ") + _bstr_t(e.Description());
MessageBox(0,bs,bstrSource, MB_OK);
//--------------------------------------------------------------CString CAdoDataModule::GetDBConnection()
{
CString strCnnString;
HRESULT hr = S_OK;
_ConnectionPtr m_pConnection;
IDataSourceLocatorPtr dlPrompt = NULL;
try
{
CREATEiNSTANCE(dlPrompt,DataLinks); // Prompt for connection information.
m_pConnection = dlPrompt->PromptNew();
// If connection object is NULL, user cancelled.
if ( NULL == m_pConnection )
return "";
// Open connection (connection returned by DataLinks is just
// a holder for the returned ConnectionString).
TESTHR(hr = m_pConnection->Open( m_pConnection->ConnectionString, L"", L"", -1 ) );
strCnnString =(char *) (m_pConnection->ConnectionString);
return strCnnString;
}
catch( _com_error &e){
HandleComError(e);
// executing the query.
if(m_pConnection != NULL) m_pConnection.Release();
return "";
}
}