同一个函数db.DoSQL();第二次调用时就出错???
错误为:
Operation for database error!
子信息添加有误!
void CPerson::OnSelchangeXinxizong()
{
CComboBox *p;
CString xinxizong;
p=(CComboBox*)GetDlgItem(IDC_XINXIZONG);
p->GetLBText(p->GetCurSel(),xinxizong);
AfxMessageBox(xinxizong);
CHandleDB db("db");
int count=0;
CString xinxifenlei;
CString leibie;
CString sql;
if(db.Connect()==false)
AfxMessageBox("数据库连接失败!");
else
{
sql="select leibie from xinxifenlei where xinxifenlei='"+xinxizong+"'";
if(db.DoSQL(sql)==false)
AfxMessageBox("数据库查询有误!");
else
{
count=db.GetRecordCount();
if(count==1)
{
leibie=db.GetFieldValue("leibie");
db.RecordSetClose();
AfxMessageBox(leibie);
CString str;
str="select xinxifenlei1 from xinxifenleizi where leibie='"+leibie+"'";
if(db.DoSQL(str)==false)//在此处错!!!
AfxMessageBox("子信息添加有误!");
else
{
AfxMessageBox("子信息添加成功!");
count=db.GetRecordCount();
for(int i=0;i<count;i++)
{
xinxifenlei=db.GetFieldValue("xinxifenlei1");
m_xinxi.AddString(xinxifenlei);
db.MoveNext();
}
}
}
}
}
}
调用函数为:
bool CHandleDB::DoSQL(CString strSql)
{
try
{
strSql.TrimLeft();
if(strSql.Left(6).CompareNoCase("select")==0) //
{
//AfxMessageBox("数据库查询成功!");
RecordSetClose();
HasOpenSet = false;
m_pRecordset->Open(_bstr_t(strSql),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
HasOpenSet = true;
}
else // not select statement
{
m_pConnection->Execute(_bstr_t(strSql),NULL,adCmdText);
}
return true;
}
catch(...)
{
AfxMessageBox("Operation for database error!");
return false;
}
}
两个表为:
表名1:xinxifenleizi
编号 leibie xinxifenlei1
1 1 调制解调器
2 1 网卡
3 2 求职
4 2 求职招聘
5 3 sdafds
6 3 gfhdfh
7 4 fsdgdfgsdf
表名2:xinxifenlei
编号 leibie xinxifenlei
1 1 通讯产品
2 2 求职招聘
3 3 房产信息
4 4 电脑产品
5 5 生活服务
6 6 艺术收藏
7 7 书籍报刊
8 8 家用电器
9 9 日用百货
10 10 音像制品
11 11 车辆船舶
12 12 摄影器材
13 13 校园专区
14 14 企业专区
8 4 44444
错误为:
Operation for database error!
子信息添加有误!
void CPerson::OnSelchangeXinxizong()
{
CComboBox *p;
CString xinxizong;
p=(CComboBox*)GetDlgItem(IDC_XINXIZONG);
p->GetLBText(p->GetCurSel(),xinxizong);
AfxMessageBox(xinxizong);
CHandleDB db("db");
int count=0;
CString xinxifenlei;
CString leibie;
CString sql;
if(db.Connect()==false)
AfxMessageBox("数据库连接失败!");
else
{
sql="select leibie from xinxifenlei where xinxifenlei='"+xinxizong+"'";
if(db.DoSQL(sql)==false)
AfxMessageBox("数据库查询有误!");
else
{
count=db.GetRecordCount();
if(count==1)
{
leibie=db.GetFieldValue("leibie");
db.RecordSetClose();
AfxMessageBox(leibie);
CString str;
str="select xinxifenlei1 from xinxifenleizi where leibie='"+leibie+"'";
if(db.DoSQL(str)==false)//在此处错!!!
AfxMessageBox("子信息添加有误!");
else
{
AfxMessageBox("子信息添加成功!");
count=db.GetRecordCount();
for(int i=0;i<count;i++)
{
xinxifenlei=db.GetFieldValue("xinxifenlei1");
m_xinxi.AddString(xinxifenlei);
db.MoveNext();
}
}
}
}
}
}
调用函数为:
bool CHandleDB::DoSQL(CString strSql)
{
try
{
strSql.TrimLeft();
if(strSql.Left(6).CompareNoCase("select")==0) //
{
//AfxMessageBox("数据库查询成功!");
RecordSetClose();
HasOpenSet = false;
m_pRecordset->Open(_bstr_t(strSql),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
HasOpenSet = true;
}
else // not select statement
{
m_pConnection->Execute(_bstr_t(strSql),NULL,adCmdText);
}
return true;
}
catch(...)
{
AfxMessageBox("Operation for database error!");
return false;
}
}
两个表为:
表名1:xinxifenleizi
编号 leibie xinxifenlei1
1 1 调制解调器
2 1 网卡
3 2 求职
4 2 求职招聘
5 3 sdafds
6 3 gfhdfh
7 4 fsdgdfgsdf
表名2:xinxifenlei
编号 leibie xinxifenlei
1 1 通讯产品
2 2 求职招聘
3 3 房产信息
4 4 电脑产品
5 5 生活服务
6 6 艺术收藏
7 7 书籍报刊
8 8 家用电器
9 9 日用百货
10 10 音像制品
11 11 车辆船舶
12 12 摄影器材
13 13 校园专区
14 14 企业专区
8 4 44444
#import "C:\Program Files\Common Files\SYSTEM\ADO\MSADO15.DLL" no_namespace rename("EOF","ADOEOF") _ConnectionPtr RTDBCON;
CString sql;
sql.Format(LOCALCON,FrontParam.LocalMDB);
MDBCON.CreateInstance(__uuidof(Connection));
MDBCON->Open(_bstr_t(sql),"","",0);
sql.Empty();
sql.Format(SCSSCON,
dbParam.SERVERAddress,
dbParam.PSDBName,
dbParam.UserID,
dbParam.PassWord);
PSDBCON.CreateInstance(__uuidof(Connection));
PSDBCON->Open(_bstr_t(sql),"","",0);
sql.Empty();
sql.Format(SCSSCON,
dbParam.SERVERAddress,
dbParam.HISDBName,
dbParam.UserID,
dbParam.PassWord);
RTDBCON.CreateInstance(__uuidof(Connection));
RTDBCON->Open(_bstr_t(sql),"","",0);
_RecordsetPtr rsPoints;
rsPoints.CreateInstance(__uuidof(Recordset));
sql="SELECT RT_Unit_ID,RT_Point_ID,EA_Unit_ID,EA_Point_ID,AP_Unit_ID,AP_Point_ID,AMMETER,COM_PORT FROM AMMETER_MAPTO_RT_EA_AP ORDER BY RT_Unit_ID,RT_Point_ID";
rs->Open(_variant_t(sql),MDBCON.GetInterfacePtr(),adOpenStatic,adLockReadOnly,adCmdText);
while(!rs->ADOEOF)
{
vVal=rs->GetCollect(_variant_t("RT_Unit_ID"));
point.RT_Unit_ID=vVal.iVal;
}
m_pRecordset->Close();
其内容为:
void CHandleDB::RecordSetClose()
{
if(HasOpenSet) m_pRecordset->Close();
}
已经关闭了!!???