ADO连接数据库有关问题
ADO连接数据库问题
我学习用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.
我学习用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.