分类: C/C++
2015-07-24 08:35:20
AfxOleInit();
HRESULT hRes;
try
{
hRes=m_pConn.CreateInstance(_T("ADODB.Connection"));
m_pConn->ConnectionTimeout = 8;
//hRes=m_pConn->Open(_bstr_t((LPCTSTR) connString),
hRes=m_pConn->Open(_bstr_t((LPCTSTR) "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=sa;Initial Catalog=jhdb;Data Source=."),
_T(""),_T(""),adModeUnknown);
if(FAILED(hRes)){
CString errormessage = _T("数据库连接失败:用户名或密码错误!");
AfxMessageBox(errormessage);
CUtilTools::MessageLog(errormessage);
m_pConn.Release();
return FALSE;
}
AfxMessageBox("连接数据库成功");
}
catch(_com_error e)///?
{
CString errormessage;
errormessage.Format(_T("连接数据库失败:%s"),e.ErrorMessage());
CUtilTools::MessageLog(errormessage);
return FALSE;
}