今天編寫一個MFC的應用程序,其中涉及到連接數據庫,單獨寫了一個類用來連接數據庫,代碼如下:
My.h
#include "stdafx.h"
class myApp : public CWinApp{
public:
_RecordsetPtr m_pRt;
_ConnectionPtr m_pConn;
public:
myApp();
virtual BOOL Instance();
void close();
};
My.cpp
#include "stdafx.h"
#include "My.h"
myApp theApp1;
BOOL myApp::Instance(){
try
{
CoInitialize(NULL);
m_pConn->ConnectionString="Provider=MSDAORA.1;Password=tiger;User ID=scott;Data Source=orcl;Persist Security Info=True";
m_pConn->Open("","","",adConnectUnspecified);
}
catch (...)
{
::MessageBox(NULL,L"連接數據庫失敗!",L"提示信息",NULL);
return FALSE;
}
return TRUE;
}
void myApp::close(){
m_pRt->Close();
m_pConn->Close();
CoUninitialize();
}
myApp::myApp(){
}
編譯的時候發生錯誤,錯誤內容是彈出一個對話框,內容是:
Debug Assertion Failed!
Program:G:\C++\New_Conn\Debug\New_Conn.exe
File:f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appcore.cpp
Line:274
For information on how your program can cause an assertion failure,see
the Visual C++ documentation on asserts.
中止 重試 忽略
以上為對話框內容
但是如果將那個
myApp theApp1; 給註釋掉的話,編譯沒錯 可以運行。請大神指教不勝感激