MS Database...

This is a discussion on MS Database... within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to make a program that connects to a database and then gets the information from it... But I'm ...

  1. #1
    the Wizard
    Join Date
    Aug 2004
    Posts
    109

    MS Database...

    I'm trying to make a program that connects to a database and then gets the information from it...
    But I'm having a problem, it compiles very well, but I get this "error", when I try to rum the program:

    Code:
    CreateInstance result=0 uuidof=1300
    Connection object created.
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    This is the code of my program:
    Code:
    #include <stdio.h>
    #include "E:\Program Files\Microsoft Visual Studio\VC98\mfc\SRC\stdafx.h"
    #import "c:\program files\common files\system\ado\msado15.dll" rename("EOF", "EOFile")
    struct StartOLEProcess {
    StartOLEProcess() {
    ::CoInitialize(NULL); 
    }
    ~StartOLEProcess() {
    ::CoUninitialize();
    }
    } _start_StartOLEProcess;
    int main() {
    ADODB::_ConnectionPtr Con = NULL;
    ADODB::_RecordsetPtr RecSet = NULL;
    ADODB::FieldPtr pQuestions;
     
    _variant_t vQuestions;
    char sQuestions[150];
    HRESULT hr = S_OK;
    VARIANT *RecordsAffected = NULL;
    hr = Con.CreateInstance(__uuidof(ADODB::Connection));
    printf("\nCreateInstance result=%d uuidof=%d\n", hr, __uuidof(ADODB::Connection));
    printf("\nConnection object created.");
    Con->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\database.mdb", "", "", 0);
     
    printf("\nConnection opened...");
    RecSet = Con->Execute("SELECT * FROM quiz", RecordsAffected, 1);
    printf("\nSQL statement processed");
    char File[10] = "Questions";
    pQuestions = RecSet->Fields->GetItem(File);
    printf("\nGetting data now...\n");
     
    while(!RecSet->EOFile) {
    vQuestions.Clear();
    vQuestions = pQuestions->Value;
    WideCharToMultiByte(CP_ACP, 0, vQuestions.bstrVal, -1, sQuestions, sizeof(sQuestions), NULL, NULL);
    printf("\n%s", sQuestions);
    RecSet->MoveNext();
    }
    printf("\n\nEnd of data..");
    RecSet->Close();
    RecSet = NULL;
    printf("\nClosed and removed recordset object.");
    Con->Close();
    Con = NULL;
    printf("\nClosed and removed connection object.");
    return 0;
    }
    Will somebody please help me out here...

    Thx in advance...
    Last edited by MipZhaP; 09-17-2004 at 12:05 PM.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Here's a working rehash of your code which is a little easier (using some helper classes instead of raw OLE data types) and a little more robust (using exception handling to report all errors that may arise)

    Code:
    #include <stdio.h>
    #include <comutil.h>
    #include <comdef.h>
    #import "c:\program files\common files\system\ado\msado15.dll" rename("EOF", "EOFile")
    
    struct StartOLEProcess 
    {
        StartOLEProcess() 
    	{
    		::CoInitialize(0); 
    	}
    	~StartOLEProcess() 
    	{
            ::CoUninitialize();
        }
    } _start_StartOLEProcess;
    
    void Test(HRESULT Res)	//Helper function to test HRESULTS and act if there's an error
    {
    	if(FAILED(Res))
    		throw _com_error(Res);
    }
    
    int main() {
    	
    	try	//If you use _com_ptr_t class, test for exceptions!
    	{
            ADODB::_ConnectionPtr Con = NULL;
    		ADODB::_RecordsetPtr RecSet = NULL;
    	
     		Test(Con.CreateInstance(__uuidof(ADODB::Connection)));
    		printf("\nConnection object created.");
    		Con->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\database.mdb", "", "", 0);
    	 
    		_variant_t Dummy;	//Nice helper class for VARIANTS
    		printf("\nConnection opened...");
    		RecSet = Con->Execute("SELECT * FROM quiz",&Dummy,ADODB::adCmdText);
    		printf("\nSQL statement processed");
    		printf("\nGetting data now...\n");
    		
    		_bstr_t Value;	//Nice herlber class for BSTR
    		while(!RecSet->EOFile) 
    		{
    			Value = RecSet->GetFields()->GetItem(L"Questions")->GetValue();
                printf("\n%s", static_cast<char*>(Value));	//Cast to ensure the right conversion takes place
    			RecSet->MoveNext();
    		}
    		printf("\n\nEnd of data..");
    		RecSet->Close();
    		RecSet = 0;
    		printf("\nClosed and removed recordset object.");
    		Con->Close();
    		Con = NULL;
    		printf("\nClosed and removed connection object.");
    		return 0;
    	}
    	catch(_com_error& e)	//This will catch errors and report them
    	{
    		printf("\nError caught - %s",e.ErrorMessage());
    	}
    }

  3. #3
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Okey, thx for your answer...
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Error connecting from Dev C++ to MS Access Database
    By Sridar in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2006, 06:09 PM
  3. CGI with MS Access database integration
    By Tarran in forum Tech Board
    Replies: 12
    Last Post: 01-11-2005, 08:47 AM
  4. Ping
    By ZakkWylde969 in forum Tech Board
    Replies: 5
    Last Post: 09-23-2003, 12:28 PM
  5. connecting to an MS Access database
    By Dimeslime in forum Windows Programming
    Replies: 0
    Last Post: 02-09-2002, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21