void CModifyRcdset::OnAppay()//是一个核心程序。
{
CString theSql; CString filename;
m_fileName.GetWindowText(filename);                     //a CEdit object in Dlg;
theSql="select * from fileindex WHERE fileindex.filename=\'"+filename+"\'";
_RecordsetPtr& m_pRecordset=getRecord(theSql);
long m_count=m_pRecordset->RecordCount;           //obtain a "m_count" ,Is the file in the fileindex table;
//////////////////////////////////////////////////////////////////////////
if(m_count>0)//file is in table,you should modify the records
{
_variant_t field=m_pRecordset->GetCollect("fileindex");
int index=field.lVal;//fetch the NO. of file in fileindex.
TRACE("%d",index);
m_pRecordset->Close();//close query at table(fileindex);
///////////获得文件序号,在插入处理时使用////////////////////////////////
_RecordsetPtr m_pTemp;     
m_pTemp.CreateInstance("ADODB.Recordset");//recordset object;
theSql.Format("select trelative.fieldkey from trelative WHERE fieldfile=%d",index);//the SQL 
m_pTemp->Open((_variant_t)theSql,
         _variant_t((IDispatch *)theApp.m_pConnection,true),
 adOpenStatic,
 adLockOptimistic,
 adCmdText);  
/////////////////////////////////////////////////////////////////////////
long nExist=m_pTemp->GetRecordCount();             //在联系表中存在的项;
int nModify=this->m_selKey.GetCount();             //在关键字列表中显示将要修改;
CString keyName;  int i; _variant_t number; //used in for(i=0;i<nModify&&i<nExist;i++);
m_pTemp->MoveFirst();
for(i=0;i<nModify&&i<nExist;i++)                   //读列表中的值修改联系表
{
m_selKey.GetText(i,keyName);
keyName="select keyindex from indexkey WHERE indexkey.keyname=\'"+keyName+"\'";
m_pRecordset=getRecord(keyName);
number=m_pRecordset->Fields->GetItem(_variant_t("keyindex"))->Value;
m_pRecordset->Close();
m_pTemp->Fields->GetItem(_variant_t("fieldkey"))->Value=number;
m_pTemp->Update();
m_pTemp->MoveNext();
}
if(i==nModify&&i<nExist)//records in database more than records must been modify;
{//set them(nExist-nModify) NULL
for( i=nModify;i<nExist;i++)
{
m_pTemp->Delete(adAffectCurrent);///删除当前记录
m_pTemp->Update();
m_pTemp->MoveNext();
}
} if(i==nExist&&i<nModify)//records should be modify more than that in database;
{//add record from NO. (trelative.recordscount+1) TO (trelative.recordscount+nModify-nExist)
_variant_t RecordsAffected;
for( i=nExist;i<nModify;i++)
{
m_selKey.GetText(i,keyName);
keyName="select keyindex from indexkey WHERE indexkey.keyname=\'"+keyName+"\'";
m_pRecordset=getRecord(keyName);
number=m_pRecordset->Fields->GetItem(_variant_t("keyindex"))->Value;
m_pRecordset->Close();
keyName.Format("insert into trelative(fieldkey,fieldfile) VALUES (%d,%d)",number.lVal,index);
theApp.m_pConnection->Execute((_bstr_t)(_variant_t)keyName,&RecordsAffected,adCmdText);
}//for
}
///////////////////更新修改数据记录完成////////////////////////////////////////////////
try
{

m_pTemp->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
}//if modify!
else//file is`t in table ,you should add file and the key_file map in trelative table;

m_pRecordset->Close();
_variant_t RecordsAffected;
m_pRecordset =theApp.m_pConnection->Execute("SELECT COUNT(*) FROM fileindex",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
TRACE("%d",vCount.lVal);
m_pRecordset->Close();///关闭记录集
//////////////////获得fileindex的记录数//////////////////////////////////////////////////
theSql.Format("insert into fileindex  VALUES (%d,%d,\'%s\')",vCount.lVal+1,vCount.lVal+1,filename);
theApp.m_pConnection->Execute((_bstr_t)(_variant_t)theSql,&RecordsAffected,adCmdText);
/////////////////添加文件到fileindex表中/////////////////////////////////////////////////
int addCount=this->m_selKey.GetCount();                  //number add to trelative;
CString keyValue; _variant_t number; 
for(int i=0;i<addCount;i++)
{
m_selKey.GetText(i,keyValue);
keyValue="select keyindex from indexkey WHERE indexkey.keyname=\'"+keyValue+"\'";
m_pRecordset=getRecord(keyValue);
number=m_pRecordset->Fields->GetItem(_variant_t("keyindex"))->Value;
m_pRecordset->Close();
keyValue.Format("insert into trelative(fieldkey,fieldfile) VALUES (%d,%d)",number.lVal,vCount.lVal);
            theApp.m_pConnection->Execute((_bstr_t)(_variant_t)keyValue,&RecordsAffected,adCmdText);
}//for
}//else
return;
}

解决方案 »

  1.   

    有点多啊,关键的问题是每次修改或者删除一个记录是不是都必须马上调用update啊。还有connection的execute对数据库的操作,和recordset打开的记录集在操作上有没有什么要 注意的。
      

  2.   

    如果你的数据库是多用户的,建议你每次修改或者删除一个记录都马上调用update。
    如果你的数据库是单用户的则不必这样做。
      

  3.   

    哦,我发觉一个如果recordset返回的是一个空记录集,调用movefirst就会错。是不是这样啊,这段代码分支多了一点,也不好测试,我把那句删掉,暂时没有错误,不知道是不是哪个原因。有必要在recordset返回后,执行movefirst吗?
      

  4.   

    如果recordset是空记录集,movefirst会报错,可以先判断recordset->adoEOF是否为空。
      

  5.   

    recordset为空集的时候,不能移动指针,否则报错。