代码如下

        _ConnectionPtr pMyConnect(__uuidof(Connection));
_RecordsetPtr m_pRecordset(__uuidof(Recordset)); // pMyConnect->ConnectionString = "Provider = SQLOLEDB; Server = .; Database = testDB; uid = sa; pwd = sa;";  CString strConnect="Provider = SQLOLEDB; Data Source = .; Initial Catalog = testDB; uid = sa; pwd = sa;"; 
CString strSel = "select * from tblPerson";

_variant_t varSel(strSel);
_variant_t varConn(strConnect);
_bstr_t bstrConn(strConnect); try
{
pMyConnect->Open(bstrConn,"sa","sa",NULL);

}
catch (_com_error &e)
{
::MessageBox(NULL, e.Description(), "警告",MB_OK|MB_ICONWARNING);
}
try
{
m_pRecordset->Open(varSel, varConn, adOpenKeyset, adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
::MessageBox(NULL, "无法打开tlbPerson表。","提示", MB_OK|MB_ICONWARNING);
}问题出在这个地方
m_pRecordset->Open大家给看一下..谢谢了

解决方案 »

  1.   

    try
        {
            m_pRecordset->Open(varSel, varConn, adOpenKeyset, adLockOptimistic, adCmdTable);
        }
        catch (_com_error &e)
        {
            e->ErrorMessage(); //看看是什么?
            ::MessageBox(NULL, "无法打开tlbPerson表。","提示", MB_OK|MB_ICONWARNING);
        }
      

  2.   

    恩 ,,打印出来了
    是  
    IDispatch error #3149.这个是不是代表第二个参数有问题啊?
      

  3.   

    Recordset.Open参数说明语法:
        recordset.Open Source, ActiveConnection, CursorType, LockType, Options参数说明:
    Source               可选,变体型,计算 Command 对象的变量名、SQL 语句、表名、存储过程调用或持久 Recordset 文件名。 ActiveConnection     可选。变体型,计算有效 Connection 对象变量名;或字符串,包含 ConnectionString 参数。
    CursorType       可选,CursorTypeEnum 值,确定提供者打开 Recordset 时应该使用的游标类型。
    可为下列常量之一(参阅 CursorType 属性可获得这些设置的定义)。
    值  常量                 说明
    --- ------------------- ----------------------- 
    0   AdOpenForwardOnly   默认值)打开仅向前类型游标。  
    1   AdOpenKeyset             打开键集类型游标。
    2   AdOpenDynamic         打开动态类型游标。
    3   AdOpenStatic               打开静态类型游标。LockType              可选。确定提供者打开 Recordset 时应该使用的锁定(并发)类型的 LockTypeEnum 值,可为下列常量之一(参见 LockType 属性可获得详细信息)。
    值  常量                                   说明
    --- -------------------------------- ----------------------- 
    1   AdLockReadOnly           (默认值)只读 — 不能改变数据。  
    2   AdLockPessimistic         保守式锁定,提供者完成确保成功编辑记录所需的工作,通常通过在编辑时立即锁定数据源的记录。
    3   AdLockOptimistic            开放式锁定(逐个) — 提供者使用开放式锁定,只在调用Update 方法时才锁定记录。
    4   AdLockBatchOptimistic  开放式批更新—用于批更新模式(与立即更新模式相对)。
      

  4.   

    varSel, varConn的值是什么?
    3149是指未指定错误...
      

  5.   

    为什么不用
    _RecordsetPtr 呢!
      

  6.   

       CString strConnect="Provider = SQLOLEDB; Data Source = .; Initial Catalog = testDB; uid = sa; pwd = sa;"; 
        CString strSel = "select * from tblPerson";
        
        _variant_t varSel(strSel);
        _variant_t varConn(strConnect);他俩的值是这个啊
      

  7.   

    pRecordset = pMyConnect->Execute("select * from tblPerson", NULL, adCmdText);
    我直接这样做.居然是对的
      

  8.   

    m_pRecordset->Open(varSel, varConn, adOpenKeyset, adLockOptimistic, adCmdTable);
    CString strConnect="Provider = SQLOLEDB; Data Source = .; Initial Catalog = testDB; uid = sa; pwd = sa;"; 
    CString strSel = "select * from tblPerson"; 
    _variant_t varSel(strSel); 
    _variant_t varConn(strConnect); -->m_pRecordset->Open(varSel, _variant_t((IDispatch *)m_pConnection,adOpenKeyset, adLockOptimistic, adCmdUnkown),