我在连接数据库时碰到了很奇怪的问题,就是在连接之前我要先打开一个"打开的模态对话框"来保存查询数据库的数据,如果不打开没有关系,一打开这个模态对话框,就会出现"未指定的错误",代码如下:
void CTranTxtDlg::OnButtonOpen()
{
static char BASED_CODE szFilter[] = TEXT("EXCEL文件(*.xls)|*.xls|All Files (*.*)|*.*||");
static char BASED_CODE lpszDefExt[] = TEXT("xls"); //Create the dialog to select the demo data file to save the result
CFileDialog dlg(TRUE,
lpszDefExt,
NULL,
OFN_HIDEREADONLY |
OFN_ENABLESIZING |
OFN_OVERWRITEPROMPT,
szFilter,
this);
m_stropen=""; if(dlg.DoModal ()==IDOK)
{
m_stropen = dlg.GetPathName ();
HWND hWnd = ::GetDlgItem (this->m_hWnd , IDC_EDIT_OPEN);
::SetWindowText (hWnd, m_stropen); }
else
{
return;
}
}
下面是调用数据库的代码:
void CTranTxtDlg::OnOK()
{
UpdateData(TRUE); CString strQuery;
CString strField;
CString strTemp;
这三个变量的获取经过检验没有错误,由于太多,在此省略,
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset)); try{ m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SARS流行与环境相关数据库.mdb;","","",0);
strQuery.Format("select %s from %s where Date=#%s#",strField,strTable1,cDate);
_bstr_t query=strQuery;
_variant_t var;
............
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
这到底是为什么啊?有谁能帮助我吗?我不胜感激!
void CTranTxtDlg::OnButtonOpen()
{
static char BASED_CODE szFilter[] = TEXT("EXCEL文件(*.xls)|*.xls|All Files (*.*)|*.*||");
static char BASED_CODE lpszDefExt[] = TEXT("xls"); //Create the dialog to select the demo data file to save the result
CFileDialog dlg(TRUE,
lpszDefExt,
NULL,
OFN_HIDEREADONLY |
OFN_ENABLESIZING |
OFN_OVERWRITEPROMPT,
szFilter,
this);
m_stropen=""; if(dlg.DoModal ()==IDOK)
{
m_stropen = dlg.GetPathName ();
HWND hWnd = ::GetDlgItem (this->m_hWnd , IDC_EDIT_OPEN);
::SetWindowText (hWnd, m_stropen); }
else
{
return;
}
}
下面是调用数据库的代码:
void CTranTxtDlg::OnOK()
{
UpdateData(TRUE); CString strQuery;
CString strField;
CString strTemp;
这三个变量的获取经过检验没有错误,由于太多,在此省略,
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset)); try{ m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SARS流行与环境相关数据库.mdb;","","",0);
strQuery.Format("select %s from %s where Date=#%s#",strField,strTable1,cDate);
_bstr_t query=strQuery;
_variant_t var;
............
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
这到底是为什么啊?有谁能帮助我吗?我不胜感激!
否则打开文件后,当前路径应该就变了,就找不到 SARS流行与环境相关数据库.mdb 文件了.
有没有 检查你的_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;的可见性??
我猜可能是那出了问题!