MFCdialog ADO 连接Access 遇到有关问题了
MFCdialog ADO 连接Access 遇到问题了
1.我已经照着网上的教程 做到如下了:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2.在APP类中已经连接号数据库了
BOOL CFamilyProManagerSoftApp::InitInstance()
{
AfxEnableControlContainer();
...
...
//Add my function_code
//初始化OLE DLLs
//AfxOleInit();//初始化COM库
AfxEnableControlContainer();//添加的初始化OLE/COM环境
if(!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL失败!");
return FALSE;
}
CString strSQL="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=FamilyProManagerDB.mdb;Persist Security Info=False";
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建connect对象
if(SUCCEEDED(hr))
{
//连接超时10S
m_pConnection->CommandTimeout = 10;
//open原型参数:字串、userID、passwd、权限
hr = m_pConnection->Open( (_bstr_t)strSQL," "," ",adModeUnknown);
}
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( "连接数据库发生异常! \r\n错误信息:%s",e.ErrorMessage( ) );
AfxMessageBox(strError); //显示错误信息
}
m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集
return FALSE;
}
3.关闭函数也添加了
int CFamilyProManagerSoftApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
//关闭并释放数据库
if (m_pConnection->State)
m_pConnection->Close();
m_pConnection->Release();
return CWinApp::ExitInstance();
}
然后,问题是我在其他对话框类中怎么操作数据库??比方说救我的一个登陆验证,要怎么再能从数据库取值?希望大神们指导一下;
还有一个小问题,那个全局对象App怎么调用会提示未定义呢?
------解决方案--------------------
在对话框CPP里加句:extern CWinApp theApp;
------解决方案--------------------
把数据库和EXE文件里放在一起,这样就可以:.\\数据库.MDB就可以了
1.我已经照着网上的教程 做到如下了:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2.在APP类中已经连接号数据库了
BOOL CFamilyProManagerSoftApp::InitInstance()
{
AfxEnableControlContainer();
...
...
//Add my function_code
//初始化OLE DLLs
//AfxOleInit();//初始化COM库
AfxEnableControlContainer();//添加的初始化OLE/COM环境
if(!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL失败!");
return FALSE;
}
CString strSQL="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=FamilyProManagerDB.mdb;Persist Security Info=False";
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建connect对象
if(SUCCEEDED(hr))
{
//连接超时10S
m_pConnection->CommandTimeout = 10;
//open原型参数:字串、userID、passwd、权限
hr = m_pConnection->Open( (_bstr_t)strSQL," "," ",adModeUnknown);
}
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( "连接数据库发生异常! \r\n错误信息:%s",e.ErrorMessage( ) );
AfxMessageBox(strError); //显示错误信息
}
m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集
return FALSE;
}
3.关闭函数也添加了
int CFamilyProManagerSoftApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
//关闭并释放数据库
if (m_pConnection->State)
m_pConnection->Close();
m_pConnection->Release();
return CWinApp::ExitInstance();
}
然后,问题是我在其他对话框类中怎么操作数据库??比方说救我的一个登陆验证,要怎么再能从数据库取值?希望大神们指导一下;
还有一个小问题,那个全局对象App怎么调用会提示未定义呢?
Access
MFC
连接
app类
------解决方案--------------------
在对话框CPP里加句:extern CWinApp theApp;
------解决方案--------------------
把数据库和EXE文件里放在一起,这样就可以:.\\数据库.MDB就可以了