-
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...
-
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());
}
}
-
Okey, thx for your answer... :)