本文介绍了ADO错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试时出错,ado.cpp:

Error when I debug, ado.cpp:

// ado.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF", "EndOfFile")


int database_execute();

int _tmain(int argc, _TCHAR* argv[])
{
  CoInitialize(NULL);

  database_execute();

  CoUninitialize();
	return 0;
}

int database_execute(){  
  _RecordsetPtr   pRecords = NULL;
  _ConnectionPtr  pConnection = NULL;
  _CommandPtr     pCommand = NULL;  
  FieldPtr        pField = NULL;

  _bstr_t         connection_string = "Provider=sqloledb;Network Library=DBMSSOCN;Data Source='127.0.0.1';Initial Catalog='DBMailServer';Integrated Security=SSPI";
  _bstr_t         query = "SELECT * FROM ABCD";
  HRESULT         hr = NULL;

  // Kết nôí Database Server: sử dụng Connection Object
  hr = pConnection.CreateInstance(__uuidof(Connection));
  hr = pConnection->Open(connection_string, "", "", adConnectUnspecified);
//
//error show that _hr return E_FAIL
//
//Unhandled exception at 0x75789617 in ado.exe: Microsoft C++ //exception: _com_error at memory location 0x001ef6fc..
//
  if(FAILED(hr)){
    printf("[+] Connection Error.\r\n");
    return -1;
  }

  // Tạo đối tượng Lệnh (Command Ọbect)
  hr = pCommand.CreateInstance(__uuidof(Command));
  pCommand->ActiveConnection = pConnection;                   // Chọn lựa Connection Object tương ứng
  pCommand->CommandText = query;                      // Câu truy vấn SQL gửi tới Database Server

  // Truy vấn SQl và nhận kết quả trong Record Object
  pRecords = pCommand->Execute(NULL, NULL, adCmdText);

  if(pRecords==NULL){
    printf("[+] Execute Error.\r\n");
    return -1;
  }

  // Lấy từng trường kết quả sử dụng đối tượng Field
  while(!(pRecords->EndOfFile))
  {

    pField = pRecords->Fields->GetItem("id");     // trường ID
    printf("id: %s\n", (LPCSTR) (_bstr_t) pField->Value);

    pField = pRecords->Fields->GetItem("name");     // trường name
    printf("name: %s\n\n", (LPCSTR) (_bstr_t) pField->Value);

    pRecords->MoveNext();         
  }

  pRecords->Close();
  pConnection->Close();

  return 0;
}

推荐答案

hr = pConnection.CreateInstance(__uuidof(Connection));


hr的值是多少,我怀疑CreateInstance失败了.


what is the value of hr, I suspect that CreateInstance is failing.


// Kết nôí Database Server: sử dụng Connection Object
hr = pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection->Open(connection_string, "", "", adConnectUnspecified);
//
//error show that _hr return E_FAIL
//



哪个呼叫返回E_FAIL?如果CreateInstance调用失败,则无法连接pConnection-> Open



Which call is returning E_FAIL? If the CreateInstance call fails, you can''t cann pConnection->Open



这篇关于ADO错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:22