Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error when I debug, ado.cpp:
C++
// 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;
}
Posted
Updated 6-Dec-10 6:32am
v2

Before your crash, line:
hr = pConnection.CreateInstance(__uuidof(Connection));

what is the value of hr, I suspect that CreateInstance is failing.
 
Share this answer
 
VB
// 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
//


Which call is returning E_FAIL? If the CreateInstance call fails, you can't cann pConnection->Open
 
Share this answer
 
hr = pConnection.CreateInstance(__uuidof(Connection));
value of hr is 'S_OK'
Thanks
Hưng
 
Share this answer
 
ADO .NET and LINQ are much cleaner than the old ADO. If you can't fix it easily, just use one of the two newer languages.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900