这是我在做一个管理系统中的部分代码,初始化COM成功,接着就弹出数据库连接失败,错误信息:未指定的错误。这是怎么回事?是不是我什么地方做错了,希望能帮帮我,谢谢。
BOOL CJXIMSApp::InitInstance()
{
if(!SUCCEEDED(CoInitialize(NULL)))
{
AfxMessageBox(_T("初始化COM库失败。"));
return FALSE;
}
else
{
AfxMessageBox(_T("初始化COM库成功。"));
}
AfxEnableControlContainer();
AfxOleInit();
//连接数据库
CString strSQL;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->CursorLocation=adUseClient;
strSQL="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=111111;Initial Catalog=JXIMS;Data Source=(local)";
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open(_bstr_t(strSQL)," "," ",-1);
}
}
catch(_com_error& e)//捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);//显示错误信息
return FALSE;
}
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif//登陆代码
CLoginDlg Logindlg;
int res=Logindlg.DoModal();
if(res==IDCANCEL)
return FALSE;
if(res==IDOK)
{
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
CString sql;
m_pConnection.CreateInstance("ADODB.Connection");
sql="SELECT * FROM UserTab WHERE NAME=' "+Logindlg.m_strUserName+" 'AND IDENTIFY=' "+Logindlg.m_strUserType+" ' ";
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
int num=m_pRecordset->GetRecordCount();
if(num>0)
{
CString strpwd;
strpwd=m_pRecordset->GetCollect("PASSWORD").bstrVal;
CString temp;
temp=Logindlg.m_strUserPassword;
if(temp.CompareNoCase(strpwd)==0)
{
CJXIMSDlg dlg;
m_pMainWnd=&dlg;int nResponse=dlg.DoModal();
if(nResponse==IDOK)
{}
else if(nResponse==IDCANCEL)
{}
}
else
{
AfxMessageBox("密码错误");
}
}
else
{
AfxMessageBox("无此用户");
}
}/*CJXIMSDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}*/// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

解决方案 »

  1.   

    就连接那部份,代码没有问题,检查一下你的SQL Server 本身的配置
      

  2.   

    你的代码 里有错误信息啊。为什么不提供出来CString errormessage;
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);//显示错误信息
    return FALSE;
      

  3.   

    errormessage.Format("连接数据库失败!\r\n错误信息:%s",(LPCTSTR)e.Description());
    改成这样就才能看到明确的错误提示。
      

  4.   

    找个ado2的ado封装类用,很方便