Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to link database to my project code in win32.Yesterday i saw below link 

https://msdn.microsoft.com/en-us/library/office/ff965871(v=office.14)#DataProgrammingWithAccess2010_ATLOLEDBExample 

but no use above code.Please help me?
Thanks in advance

What I have tried:

I tried above link with different databases but not work.


This link also not work

https://msdn.microsoft.com/en-us/library/cc811599.aspx#Y3869

[EDIT by Jochen Arndt: Inserted code and text posted as solution]
C++
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

hr=S_OK;
try
{
    CoInitialize(NULL);
    _bstr_t strCnn("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb");
    _RecordsetPtr pRstAuthors=NULL;
     
    _ConnectionPtr  pConnection=NULL;
     
    // Call Create instance to instantiate the Record set
    hr = pRstAuthors.CreateInstance(__uuidof(Recordset));
    pConnection.CreateInstance(_uuidof(Connection));
    pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb";
    pConnection->ConnectionTimeout = 30;  
    if(FAILED(hr))
    {
        // printf("Failed creating record set instance\n");
        return;
    }
    //Open the Record set for getting records from Customer table
    pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",adOpenStatic,adLockReadOnly, adCmdText);
 
    _bstr_t valField1;
    int valField2;
    pRstAuthors->MoveFirst();
    if (!pRstAuthors->EndOfFile)
    {
        while(!pRstAuthors->EndOfFile)
        {
            valField1 = pRstAuthors->Fields->GetItem("username")->Value.intVal;
        }
    }
}
catch(_com_error & ce)
{
    //sprintf("Error:%s\n",ce.Description);
}
CoUninitialize();

pRstAuthors->Open: In this method i got error.
[/EDIT]
Posted
Updated 7-Aug-17 23:27pm
v3
Comments
Richard MacCutchan 8-Aug-17 3:39am    
What does "no use above code" mean?
Richard MacCutchan 8-Aug-17 5:30am    
Why are you hard coding the connection string in three different places?
Richard MacCutchan 8-Aug-17 5:33am    
You are calling a method on pRstAuthors but you never initialise it to point to anything. And looking at the rest of the code there are other similar errors. Did you actually write this code or is it just copied from the internet even though incomplete?

See this article: Developing Access 2007 Solutions with Native C or C++[^]
See here for connection details with Provider=Microsoft.ACE.OLEDB.12 : Microsoft ACE OLEDB 12.0 Connection Strings - ConnectionStrings.com[^]

Example:
C#
_bstr_t strCnn("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb;Persist Security Info=False;");
 
Share this answer
 
v3
Comments
sankars3 8-Aug-17 5:23am    
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")


hr=S_OK;
try
{
CoInitialize(NULL);
_bstr_t strCnn("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb");
_RecordsetPtr pRstAuthors=NULL;

_ConnectionPtr pConnection=NULL;
 
// Call Create instance to instantiate the Record set
hr = pRstAuthors.CreateInstance(__uuidof(Recordset));
pConnection.CreateInstance(_uuidof(Connection));
pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb";
pConnection->ConnectionTimeout = 30;

if(FAILED(hr))
{
// printf("Failed creating record set instance\n");
return;
}

//Open the Record set for getting records from Customer table

pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",adOpenStatic,adLockReadOnly, adCmdText);
 
_bstr_t valField1;
int valField2;

pRstAuthors->MoveFirst();
 
if (!pRstAuthors->EndOfFile)
{
 
while(!pRstAuthors->EndOfFile)
{
valField1 = pRstAuthors->Fields->GetItem("username")->Value.intVal;
}
}
}
catch(_com_error & ce)
{

//sprintf("Error:%s\n",ce.Description);
}
CoUninitialize();

above code i got a error pRstAuthors->Open method.Please help RickZeeland.Thanks for advacne.
RickZeeland 8-Aug-17 5:34am    
You might need to add 'Persist Security Info=False;'
sankars3 9-Aug-17 9:14am    
where i put this one.please help me?
It is all contained in the link from your question.

You have to decide which method you want to use:

  • Direct DAO (Win32)
  • ATL OLE DB (ATL/MFC)
  • ADO (Win32)
  • Direct ODBC (Win32)
  • MFC ODBC (MFC)

The Win32 methods require importing of a DLL which might be a bit tricky to avoid name conflicts.

Depending on the used method it might be necessary to ship additional packages with your application to ensure that the used database interface is installed on the client (Microsoft Access Redistributables or MDAC which should be present with all recent Windows versions since Vista/7).

See also the notes about 32/64 bit. If possible build a 32-bit application.

DAO provides the best support (more functions than the other methods). When using MFC, there are classes for OLE DB and ODBC which reduces the amount of code to write.

If you got stuck with a specific method, ask again or update your question with a short code example and a detailed problem description. "Not work" does not mean anything to us; especially when not having a single piece of code and an error message.

[EDIT: For code posted meanwhile]
If a function returns some kind of status / error, check that and report errors immediately. Your code snippet does not do that for all functions and reports errors sometimes after calling other function meanwhile. Read also the documentation for the used function to know which parameters has to be passed.

You have also to follow the examples regarding the order of the functions to be called:

  • Create a _ConnectionPtr
  • Open the connection
  • Create a RecordsetPtr
  • Open the recordset passing the connection

In detail:
pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",adOpenStatic,adLockReadOnly, adCmdText);
is not a valid call. It misses passing the connection. It must be:
pRstAuthors->Open("SELECT* FROM Customers","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Sankar\\Database1.accdb",
_variant_t((IDispatch *) pConnection, true),
adOpenStatic,
adLockReadOnly, 
adCmdText);
[/EDIT]
 
Share this answer
 
v2
Comments
sankars3 8-Aug-17 4:24am    
Yesterday I Used ATL OLEDB and ADO and Direct ODBC.These three databases not worked properly in my system.
Mohibur Rashid 8-Aug-17 4:36am    
Not worked properly is not enough to offer help. You need to share the error message too
Jochen Arndt 8-Aug-17 4:50am    
I have used all these methods in the past. So they do work.

If they do not work for you, you must have made something wrong.
But we can't help you while you did not tell us in detail what you have done and what you got (especially full error message and where that occured).
sankars3 8-Aug-17 5:11am    
hr = dbDataSource.OpenFromInitializationString(lpcOleConnect);
in above code i got error.In that i tried different methods

hr=dbDataSource.OpenFromFile(lpoleConnect);
Thanks for your help Jochen Arndt..
sankars3 8-Aug-17 5:57am    
error:CoInitialized not called in datasource.

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