我学习用ADO连接数据库,在连接时可以正常连接,但是在查询表的时候却不能正常Open,
recPtr->Open(strSQL.AllocSysString(), 
pApp->m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
经过调试,发现pApp->m_pConnection.GetInterfacePtr()的值为NULL所以来导致的错误,但是去不清楚为什么它的值会是空。
我使用的单文档程序,在APP类中连接的数据库:
bool CLiCaiApp::ConnectionDb()
{
m_pConnection.CreateInstance(_uuidof(Connection));
CString strSQL;
strSQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=LiCaiDB.mdb";
try
{
m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);
}
catch (_com_error e)
{
CString strError;
strError.Format("打开连接发生异常。%s",e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
BOOL CLiCaiApp::InitInstance()
{
AfxEnableControlContainer();
//初始化OLE DLLS
if(!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL失败。");
return FALSE;
}
// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings();  // Load standard INI file options (including MRU) // Register the application's document templates.  Document templates
//  serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CLiCaiDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CLiCaiView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
if (!ConnectionDb())
{
AfxMessageBox("连接数据库失败。");
// return FALSE;
}
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->SetWindowText("简单理财");
m_pMainWnd->UpdateWindow();
return TRUE;
}
请高手指点指点错误出在哪?

解决方案 »

  1.   

    连接数据库不要放在BOOL CLiCaiApp::InitInstance()里。
      

  2.   

    bool CLiCaiApp::ConnectionDb()
    {
    m_pConnection.CreateInstance(_uuidof(Connection));
    //我又在连接数据库时加上了这几句,发现是可以得到这个指针的
    //为什么在那里面得不到呢?难道是AfxGetApp()不对吗?
    if (m_pConnection.GetInterfacePtr()==NULL)
    {
    AfxMessageBox("Can not GetInterfacePtr");
    }
    CString strSQL;
    strSQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=LiCaiDB.mdb";
    try
    {
    m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);
    }
    catch (_com_error e)
    {
    CString strError;
    strError.Format("打开连接发生异常。%s",e.ErrorMessage());
    AfxMessageBox(strError);
    return FALSE;
    }
    return TRUE;
    }
      

  3.   

    我又调试了一下,发现在除了CLiCaiApp以外,其余的类里都不能用全局变量theApp这又是怎么回事
      

  4.   

    1.我做的都是初始话COM库的,你这里初始化OLE应该也可以。
    2.theApp,你在其他地方用,需要在文件头上加上extern
      

  5.   

    ((CLiCaiApp*)pApp)->m_pConnection.GetInterfacePtr()