4,011
社区成员
发帖
与我相关
我的任务
分享
bool Connect(ATL::CDataSource* pDataSource,CSession* pSession)
{
ASSERT(pDataSource);
CComBSTR bstrServer(m_strServerName);
CComBSTR bstrUser(m_strLoginName);
CComBSTR bstrPassword(m_strPassword);
CComBSTR bstrDatabase(m_strDatabaseName);
if (pSession && pSession->m_spOpenRowset != NULL)
pSession->m_spOpenRowset.Release();
CDBPropSet dbinit(DBPROPSET_DBINIT);
dbinit.AddProperty(DBPROP_AUTH_PASSWORD, bstrPassword);
dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
dbinit.AddProperty(DBPROP_AUTH_USERID, bstrUser);
dbinit.AddProperty(DBPROP_INIT_CATALOG, bstrDatabase);
dbinit.AddProperty(DBPROP_INIT_DATASOURCE, bstrServer);
dbinit.AddProperty(DBPROP_INIT_LCID, (long)1049);
dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
if(FAILED(pDataSource->Open(_T("SQLOLEDB.1"), &dbinit)))
{
CString strMsg = "Cannot connect to data source";
strMsg += "\nServer\t= " + m_strServerName;
strMsg += "\nDatabase\t= " + m_strDatabaseName;
strMsg += "\nLogin\t= " + m_strLoginName;
AfxMessageBox(strMsg);
if(pDataSource)
{
pDataSource->Close();
}
goto ErrorExit;
}
else
{
if (pSession && pSession->Open(*pDataSource) != S_OK)
{
AfxMessageBox(_T("Couldn't create session on data source"));
goto ErrorExit;
}
}
return true;
ErrorExit:
CloseConnection();
return false;
}
bool Connect(ATL::CDataSource* pDataSource,CSession* pSession)
{
ASSERT(pDataSource);
CComBSTR bstrServer(m_strServerName);
CComBSTR bstrUser(m_strLoginName);
CComBSTR bstrPassword(m_strPassword);
CComBSTR bstrDatabase(m_strDatabaseName);
if (pSession && pSession->m_spOpenRowset != NULL)
pSession->m_spOpenRowset.Release();
CDBPropSet dbinit(DBPROPSET_DBINIT);
//Setting
dbinit.AddProperty(DBPROP_AUTH_CACHE_AUTHINFO, true);
dbinit.AddProperty(DBPROP_AUTH_ENCRYPT_PASSWORD, false);
dbinit.AddProperty(DBPROP_AUTH_MASK_PASSWORD, false);
dbinit.AddProperty(DBPROP_AUTH_PASSWORD, bstrPassword);
dbinit.AddProperty(DBPROP_AUTH_USERID, bstrUser);
dbinit.AddProperty(DBPROP_INIT_DATASOURCE, bstrDatabase);
dbinit.AddProperty(DBPROP_INIT_MODE, (long)3);
dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
dbinit.AddProperty(DBPROP_INIT_PROVIDERSTRING, OLESTR(""));
dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
if(FAILED(pDataSource->Open(_T("Microsoft.Jet.OLEDB.4.0"), &dbinit)))
{
CString strMsg = "Cannot connect to data source";
strMsg += "\nServer\t= " + m_strServerName;
strMsg += "\nDatabase\t= " + m_strDatabaseName;
strMsg += "\nLogin\t= " + m_strLoginName;
AfxMessageBox(strMsg);
if(pDataSource)
{
pDataSource->Close();
}
goto ErrorExit;
}
else
{
if (pSession && pSession->Open(*pDataSource) != S_OK)
{
AfxMessageBox(_T("Couldn't create session on data source"));
goto ErrorExit;
}
}
return true;
ErrorExit:
CloseConnection();
return false;
}
HRESULT hr = pDataSource->Open(_T("Microsoft.Jet.OLEDB.4.0"), &dbinit));
if (FAILED(hr))
{
_com_error e(hr);
AfxMessageBox(e.ErrorMessage());
AfxMessageBox(e.Description());
return;
}