各位高手,我第一次开发数据库应用,利用的是ADO连接ACCESS数据库,按照书上说法获得_ConnectionPtr后,创建_RecordsetPtr实例,然后Recordset 的open调用可以执行SQL来获取记录集。可是我下面的程序为什么不能使用SQL呢,但是在Open里面直接填入表名就能获得整个表的记录集。即使用最简单的SQL“SELECT * FROM mydb”也会报“FROM子句语法错误 ”:(程序如下:
获得_ConnectionPtr: CString lpszFile = m_strPath + "\\db.mdb";
connect.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;Jet OLEDB:Database Password=%s",lpszFile,szPassword);
HRESULT hr;
// Create new Connection Object
hr = m_pConnection.CreateInstance(__uuidof( Connection ));
if (SUCCEEDED(hr))
{
try
{ // Open Connection to database
hr = m_pConnection->Open((_bstr_t)connect,"","",adModeUnknown);///连接数据库
if (SUCCEEDED(hr))
{ // Successful connection
m_IsConnected = TRUE; // Set connection state
}
}
catch ( _com_error &e ){
DisplayError(e);
return FALSE;
}
}_ConnectionPtr是没问题的。下面取_RecordsetPtr,如果直接以表名为Open的第一个参数也没问题。但是SQL语句就不行:_RecordsetPtr CmyDB::GetRecordset(LPCTSTR szSql)
{
_RecordsetPtr pRecordset;
if(FAILED(pRecordset.CreateInstance( __uuidof( Recordset ))))
{
m_initialized=FALSE;
return NULL;


try{
pRecordset->Open(_variant_t(szSql),
            _variant_t((IDispatch *)m_pConnection,true), adOpenKeyset,
            adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
DisplayError(e);
return NULL;
}
return pRecordset;
}SQL“SELECT * FROM mydb” 调用失败后,跳入catch块,错误显示如下:
:连接数据库错误 错误代码 = 80040e14  Msg: IDispatch error #3092
 来源: Microsoft JET Database Engine
 内容: FROM 子句语法错误。倾听高手解答

解决方案 »

  1.   

    adOpenKeyset, adLockOptimistic, adCmdTable
    ---------------------------------------------------一般不用来保存的话,我使用只进游标,以adCmdText方式打开数据集。
    Keyset很少用到。
      

  2.   

    没有问题的 看看下面的代码 都通过测试的_ConnectionPtr  m_pConnection;  App::InitInstance()HRESULT hr;
    try
    {
        hr= m_pConnection.CreateInstance ("ADODB.Connection");
        if(SUCCEEDED(hr))
        {
          hr= m_pConnection->Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=tt.mdb","","",adModeUnknown);
         }
    }
    catch(_com_error e)
    {
       CString errormessage;
       errormessage.Format("数据库连接失败!\r\n错误信息:%s",e.ErrorMessage());
       AfxMessageBox(errormessage);
    }App::ExitInstance() if(m_pConnection)
    {
    m_pConnection->Close();
    m_pConnection = NULL;
    }数据库处理类_RecordsetPtr  m_pRecordset;if((m_pRecordset!=NULL )&& (m_pRecordset->GetState ()!=adStateClosed))
    m_pRecordset->Close ();CString strsql;
    strsql.Format ("select * from tablename where columnName = '"+strName+"'");
    try
    {
    m_pRecordset->Open (_variant_t(strsql),_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    }
    catch(_com_error e)
    {
    AfxMessageBox("数据库连接失败!");
    return;
    }
    这样做没问题 但比较复杂
    我建议你用人家封装好的ADO 处理类 网上有很多的
      

  3.   

    SELECT * FROM mydbmydb 看名字好像是一个数据库名
    但这里应该是一个表名!select * from mytable
      

  4.   

    pRecordset->Open(_variant_t(szSql),
                _variant_t((IDispatch *)m_pConnection,true), adOpenKeyset,
                adLockOptimistic, adCmdTable);
    ////////////////////////////////////////////////////////////
    对于Open 这个函数当第一个参数为数据表名称时,最后一个参数为adCmdTable;
    当第一个参数为语句时,最后一个参数为了adCmdText;另外当你在调用这个函数时,如何前面已经调用过并且没关闭,要先用Close() 函数关闭一下试一试,祝你好运!!!