MFC问题建立了一个基于对话框的MFC程序,想用ADO来连接数据库
我在CBankOfSEUApp::InitInstance()添加了代码。但好像不能连接数据库,而且程序也不报错,即使找不到数据库文件也不会报错误。我不知道哪里错了,是不...
我在CBankOfSEUApp::InitInstance()添加了代码。但好像不能连接数据库,而且程序也不报错,即使找不到数据库文件也不会报错误。我不知道哪里错了,是不是在对话框的类中不能连接ADO数据库?
BOOL CBankOfSEUApp::InitInstance()
{
AfxEnableControlContainer();
// 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
CBankOfSEUDlg 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
}
:: CoInitialize(NULL);
try
{
//创建连接对象
m_pConnection.CreateInstance("ADODB.Connection");
//打开本地Access 库ClientData.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ClientData.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库路径是否正确!");
return FALSE;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return TRUE;
} 展开
BOOL CBankOfSEUApp::InitInstance()
{
AfxEnableControlContainer();
// 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
CBankOfSEUDlg 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
}
:: CoInitialize(NULL);
try
{
//创建连接对象
m_pConnection.CreateInstance("ADODB.Connection");
//打开本地Access 库ClientData.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ClientData.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库路径是否正确!");
return FALSE;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return TRUE;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询