Sql server 为什么OLE DB IRowset.GetNextRows访问冲突

Sql server 为什么OLE DB IRowset.GetNextRows访问冲突,sql-server,com,oledb,Sql Server,Com,Oledb,我正在使用OLE DB从SQL server检索一些数据,代码的概要如下 m_spCmd->SetCommandText(DBGUID_DEFAULT,L"SELECT * FROM sys.DM_OS_PERFORMANCE_COUNTERS"); // S_OK CComPtr<IRowset> result; m_spCmd->Execute(nullptr,__uuidof(IRowset),nullptr,nullptr,(IUnknown*

我正在使用OLE DB从SQL server检索一些数据,代码的概要如下

m_spCmd->SetCommandText(DBGUID_DEFAULT,L"SELECT * FROM sys.DM_OS_PERFORMANCE_COUNTERS"); // S_OK
CComPtr<IRowset> result;
m_spCmd->Execute(nullptr,__uuidof(IRowset),nullptr,nullptr,(IUnknown**)&result); // S_OK
DBCOUNTITEM fetched;
HROW *hrow;
result->GetNextRows(DB_NULL_HCHAPTER,0,1,&fetched,&hrow);  // A
  • sqlcmd.cpp
  • 编撰
  • 字体这样说

    如果*prghRows不是输入上的空指针,则它必须是指向的指针 使用者分配的内存足够大,可以返回 请求的行数。如果使用者分配的内存较大 如果不需要,则提供程序将按照 pcrowsobained;剩余内存的内容未定义

    所以不能在输入上传递随机指针。必须将其设置为有效或空值:

    HROW* hrow = NULL;
    

    你有完整的小型复制代码吗?上传代码供审查:)
    #include "sqlcmd.h"
    
    template<> 
    void SQLCmd::print(CComPtr<IRowset> ptr)
    {
      DBORDINAL count;
      DBCOLUMNINFO *info;
      OLECHAR *buf;
      
      auto accessor = CComQIPtr<IAccessor>(ptr);
      auto colinfo = CComQIPtr<IColumnsInfo>(ptr);
      colinfo->GetColumnInfo(&count,&info,&buf);
      
      /*
      DBBINDING *bindings = new DBBINDING[count];
      memset(bindings,0,sizeof(DBBINDING)*count);
    
      DBBINDSTATUS *bindingstatus = new DBBINDSTATUS[count];
      memset(bindingstatus,0,sizeof(DBBINDSTATUS)*count);
      
      DBBYTEOFFSET offset = 0;
      int rowsize = 0;
      for(int i = 0 ; i < count ; i++)
      {
        auto &[pwszName,pTypeInfo,iOrdinal,dwFlags,ulColumnSize,wType,bPrecision,bScale,columnid] = info[i];
        bindings[i].iOrdinal = iOrdinal;
        bindings[i].obValue = offset;
        bindings[i].wType = wType;
        bindings[i].dwPart = DBPART_VALUE;
        bindings[i].bPrecision = bPrecision;
        bindings[i].bScale = bScale;
        offset += ulColumnSize;
        rowsize += ulColumnSize;
        printf("%ws %lld %d\n",pwszName,ulColumnSize,wType);
      }
      
      HACCESSOR haccessor;
      hr = accessor->CreateAccessor(DBACCESSOR_ROWDATA,count,bindings,rowsize,&haccessor,bindingstatus);
      printf("CreateAccessor %x %llx\n",hr,haccessor);
      for(int i = 0 ; i < count ; i++)
        if(DBBINDSTATUS_OK != bindingstatus[i])
          printf("%d - %d\n",i,bindingstatus[i]);*/
      
      DBCOUNTITEM fetched;
      HROW *hrow;
      printf("before GetNextRows\n");
      hr = ptr->GetNextRows(DB_NULL_HCHAPTER,0,1,&fetched,&hrow);  
      printf("GetNextRows %x %lld\n",hr,fetched);
    }
    
    SQLCmd::SQLCmd()
    {
      GUID msoledbsql;
      CLSIDFromString(L"{5A23DE84-1D7B-4A16-8DED-B29C09CB648D}",&msoledbsql);
      m_spDb.CoCreateInstance(msoledbsql);
      
      m_spDbProperty = CComQIPtr<IDBProperties>(m_spDb);
      CDBPropSet set(DBPROPSET_DBINIT);
      set.AddProperty(DBPROP_AUTH_INTEGRATED,L"SSPI");
      set.AddProperty(DBPROP_INIT_CATALOG,L"master");
      set.AddProperty(DBPROP_INIT_DATASOURCE,L"localhost");
      m_spDbProperty->SetProperties(1,&set);
      
      m_spDb->Initialize();
    
      auto m_spCrsession = CComQIPtr<IDBCreateSession>(m_spDb);
      if (!!m_spCrsession)
      {
        hr = m_spCrsession->CreateSession(nullptr,__uuidof(ISessionProperties),(IUnknown**)&m_spSsProperty);
        auto _ = CComQIPtr<IDBCreateCommand>(m_spSsProperty);
        if (!!_)
          hr = _->CreateCommand(nullptr,__uuidof(ICommandText),(IUnknown**)&m_spCmd);      
      }
      
      CComPtr<IRowset> result;
      hr = m_spCmd->SetCommandText(DBGUID_DEFAULT,L"SELECT * FROM sys.DM_OS_PERFORMANCE_COUNTERS");
      printf("SetCommandText 0x%x\n",hr);
      DBROWCOUNT rowcount;
      hr = m_spCmd->Execute(nullptr,__uuidof(IRowset),nullptr,&rowcount,(IUnknown**)&result);
      printf("Execute 0x%x %lld\n",hr,rowcount);
      if (!!result)
      {
        print(result);
      } 
    }
    
    SQLCmd::~SQLCmd()
    {
      if (!!m_spDb) m_spDb->Uninitialize();
    }
    
    #include "sqlcmd.h"
    int main()
    {
      CoInitialize(nullptr);
      SQLCmd cmd;
    }
    
    cl /nologo /EHsc /std:c++latest test.cpp sqlcmd.cpp /link /debug
    
    >test
    SetCommandText 0x0
    Execute 0x0 -1
    before GetNextRows
    
    HROW* hrow = NULL;