使用odbc与数据库access连接,正常连接上了
现在想查找一下context字段的内容,但是查找的时候总是提示我说检索记录错误,找了不少的资料也没解决,请求这里的高人指导
另外连接数据库的时候,前面一个方法已经正常调用了,也能正常的取得数据,插入数据库,但是再一次取得的话,就不可以,请问是什么原因呢?代码如下:
CString strSQLselect=_T("");
USER_INF userInf;
CString strContext =_T(""); try{
strSQLselect.Format("Select * FROM chatcontext WHERE power = '%s' order by data_t desc","Student");

if(m_pSet->IsOpen())            
   m_pSet->Close();

m_pSet -> Open(CRecordset::dynaset,strSQLselect);
if(!(m_pSet->IsEOF()))     //  从这里开始设置的断点,没有任何执行,但是m_pSet有值
{
m_pSet -> GetFieldValue("context",strContext);  // get the value

strcpy(userInf.context,strContext);
}                                       ///  到这里一直没有执行 
}catch (CException *e){                //   直接跳到这里  执行了  
 
TCHAR   szError[1024];  
e->GetErrorMessage(szError,1024);   
::AfxMessageBox(szError); 

}相反,一下代码可以正常执行,不明白原因,是我对于数据库操作的地方理解有问题 ?  请高人指教
CString strSQLselect=_T("");
CString strSQLinsert=_T("");
USER_INF userInf; strSQLselect.Format("Select * FROM userinfo WHERE id = %ld",m_nLocalId);
if(m_pSet->IsOpen())
   m_pSet->Close();

m_pSet -> Open(CRecordset::dynaset,strSQLselect);
if(!(m_pSet->IsEOF()))
{
m_pSet -> GetFieldValue("power",strPower);    // get the value
strcpy(userInf.power,strPower);
}

strSQLinsert.Format("insert into chatcontext(Localname,Targetname,context,data_t,power) values('%s','%s','%s','%s','%s')",
m_strLocalNickname,m_strTargetNickname,buff,str_time,userInf.power);
if (NULL != m_pSet)
{
if(m_pSet->IsOpen())
m_pSet->Close(); if ( !m_pSet->IsOpen()) // if the recordset isn't already open..
m_pSet->Open(CRecordset::dynaset,"select * from userinfo"); m_pSet->m_pDatabase->ExecuteSQL(strSQLinsert);
m_pSet->Close();
nRetVal = REGT_SUCCESS;
}
else{

AfxMessageBox("Data is NULL,please check the data!!!");
}

解决方案 »

  1.   

    power 是不是数据库内置函数?表明、字段最好用中括号括起来,象这样:SELECT * FROM [TabName] ...
      

  2.   

    power是定义的结构体的内容,不用中括号也可以取得值的吧?
      

  3.   

    access 数据表设置主键啦吗
      

  4.   

    strSQLselect.Format("Select * FROM chatcontext WHERE power = '%s' order by data_t desc","Student");
    这句中的sql直接在数据库里能不能执行,在运行时打印出sql语句,放在数据库里看能否执行
      

  5.   

    在access中直接可以运行,可以查到我想要的数据信息,但是放在程序中就无法得到我想要的结果,不知道为什么,而且在
    if(!(m_pSet->IsEOF())) // 从这里开始设置的断点,没有任何执行,但是m_pSet有值
    {
    m_pSet -> GetFieldValue("context",strContext); // get the valuestrcpy(userInf.context,strContext);
    } /// 到这里一直没有执行 
    }catch (CException *e){ // 直接跳到这里 执行了   
    ....
    }
    以上if部分根本没有执行,直接就跳出执行catch的部分
      

  6.   

    strSQLselect.Format("Select * FROM chatcontext WHERE power = '%s' order by data_t desc","Student");改成trSQLselect.Format("Select * FROM chatcontext WHERE power = %s order by data_t desc","Student");
    试试看
      

  7.   

    感谢回复,不管用的,另外我的Student是字符串,不是变量,所以要加''的啊
      

  8.   

    你自己调试下,如果按你那样写,查询的字符串是Select * FROM chatcontext WHERE power = 'Student',就算你的student是个变量,那么调试的字符串也是带单引号的
    你把单引号去掉应该才能检索到记录的
      

  9.   

    你试试看在try部分里多打几个断点,看看if语句前面的执行有没有问题,或者你下面可以正常执行的代码也加个try语句块去试试看