我学习用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;
}
请高手指点指点错误出在哪?
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;
}
请高手指点指点错误出在哪?
解决方案 »
- typedef ISeal * (* TCreateGdfSeal)(void);
- DrawText 多行显示怎样设置间距?
- Dll中的某个数据更新之后,如何即时通知调用这个Dll的exe主程序接受并执行相应操作呢??可以直接发消息过去通知??
- 想用 PreTranslateMessage 对 键盘上的Delete键进行限制操作,怎么试都不行啊,我是在Dialog里,不想用Onchar和Onkeydo
- ADO连接access数据出错,#error : WINDOWS.H already included. MFC apps must not #includ
- 请大家讨论一下关于目前COM应用的前景问题
- ado操纵数据库,怎样实现对数据库表的修改,比如说新增加一个属性列?谢谢!
- View显示全屏的问题
- BSTR数据类型问题
- 非阻塞式套接字的接收
- 过年了,电脑也坏了,有部分棋牌游戏源码跟通讯类源码,求各位老大支持点买台电脑!
- 自己编写的建模工具,画出图形后如何导出成Pdf格式的?
{
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;
}
2.theApp,你在其他地方用,需要在文件头上加上extern